1. Method One
namespace App\providers;...classAuthserviceproviderextendsserviceprovider{... Public functionBoot (gatecontract$gate) {Parent:: Registerpolicies ($gate); Defining authorization Capabilities$gate-Define(' Update-post ',function($user,$post){ return $user->id = =$post-user_id; }); }}
namespace App\http\controllers;...//Verify authorization in the controllerclassPostcontrollerextendscontroller{ Public functionShow$id) { //auth ()->logout ();Auth ()->loginusingid (3); $post= Post::findorfail ($id); //if (Gate::d enies (' Show-post ', $post)) {//Abort (403, ' Sorry, not sorry! '); }//$this->authorize (' Update-post ', $post); returnView (' Posts.show ',Compact(' Post ')); }}
Verify authorization in view and Show Update link if passed
$post->title}}@can ($post)<a href=# >update</a>@endcan
2. Method Two
Establish policy
PHP Artisan Make:policy Postpolicy
Registered
namespace App\providers; ... class extends serviceprovider{ protected$policies = [ // ' app\model ' = > ' App\policies\modelpolicy ', \app\post::class = \app\policies\postpolicy: : Class, ];
Defining class policy functions
namespace App\policies; Use App\user; Use App\post; class postpolicy{ // can establish multiple test methods corresponding to different scenes public function$ User$post) { return$user$post user_id;} }
Use the same method as above.
Laravel Authorize (Authorized)