Laravel/PHP Articles

Articles from Laravel/PHP sites on the web

Laravel/PHP Articles
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.

Sign Up For Our Newsletter

Each Week, We curate the top Laravel/PHP articles, jobs, events and More!

We'll send you a weekly email with the latest Laravel/PHP articles, jobs and events. Please review our Code of Conduct in the meantime. Talk to you soon!