Laravel Form Request(s) Using Form Request


Example

Lets say continue with User example (you may have controller with store method and update method). To use FormRequests you use type-hinting the specific request.

...

public function store(App\Http\Requests\StoreRequest $request, App\User $user) { 
    //by type-hinting the request class, Laravel "runs" StoreRequest 
    //before actual method store is hit

    //logic that handles storing new user 
    //(both email and password has to be in $fillable property of User model
    $user->create($request->only(['email', 'password']));
    return redirect()->back();
}

...

public function update(App\Http\Requests\UpdateRequest $request, App\User $users, $id) { 
    //by type-hinting the request class, Laravel "runs" UpdateRequest 
    //before actual method update is hit

    //logic that handles updating a user 
    //(both email and password has to be in $fillable property of User model
    $user = $users->findOrFail($id);
    $user->update($request->only(['password']));
    return redirect()->back();
}