使用表單請求

讓我們繼續使用 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();
}