使用表单请求

让我们继续使用 User 示例(你可以使用存储方法和更新方法的控制器)。要使用 FormRequests,请使用类型提示特定请求。

...

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();
}