Category: Laravel

One of the new features in Laravel 5.8 allows you to not register your policies in AuthServiceProvider, they will be “guessed” automatically.
Fill in the policy with exact methods and rulesuse App\User; use App\Post; class PostPolicy { public function update(User $user, Post $post) { return $user->id === $post->user_id; } }Step 3.
Register policies (this is needed only before Laravel 5.8)use App\Post; use App\Policies\PostPolicy; class AuthServiceProvider extends ServiceProvider { protected $policies = [ Post::class => PostPolicy::class, ];So, from Laravel 5.8 you don’t need to do step 3, system will recognize the policies automatically.
Here’s how that “guessing” function actually looks in Laravel internals:protected function guessPolicyName($class) { return dirname(str_replace('\\', '/', $class)).'\\Policies\\'.class_basename($class).
'Policy'; }So your Policy should be in app/Policies folder and has the same name as model, with Policy suffix.

Get the latest Laravel/PHP jobs, events and curated articles straight to your inbox, once a week

Community Partners