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.
Newsletter

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

Glimpse streamlines Laravel development by seamlessly deploying GitHub pull requests to preview environments with the help of Laravel Forge. Glimpse streamlines Laravel development by seamlessly deploying GitHub pull requests to preview environments with the help of Laravel Forge.
Fathom Analytics | Fast, simple and privacy-focused website analytics. Fathom Analytics | Fast, simple and privacy-focused website analytics.
Shirts painstakingly handcrafted by under-caffeinated developers. Shirts painstakingly handcrafted by under-caffeinated developers.
Community Partners