<?phpnamespaceApp\Policies;useApp\User;useApp\Post;classPostPolicy{/** * Determine if the given post can be updated by the user. * * @param \App\User $user * @param \App\Post $post * @return bool */publicfunctionupdate(User$user,Post$post){return$user->id===$post->user_id;}}
Controller 中使用如下:
123456789101112131415161718192021222324
<?phpnamespaceApp\Http\Controllers;useApp\Post;useIlluminate\Http\Request;useApp\Http\Controllers\Controller;classPostControllerextendsController{/** * Update the given blog post. * * @param Request $request * @param Post $post * @return Response */publicfunctionupdate(Request$request,Post$post){$this->authorize('update',$post);// The current user can update the blog post...}}