Laravel policies

 Laravel policies help helps to manage the authorize user actions.


Create Policy: To create run a command php artisan make:policy PostPolicy or run it will model php artisan make:policy PostPolicy --model=Post 

Register Policy:  A policy can be registered in the AuthServiceProvider. So open this file and there is a default array of policies. Now add your policy with model here like:-

Make Condition in Policy: Write a function which return boolean and use this function in blade or controller to mange the authorize user actions

 

In above example only post author can update the post.

Use policy in controller: Check user is a author of post before edit a post by authorize() in controller.

Use policy in blade: To manage the accessibility of blade elements by policy use @can() method.

This is the all about policy and it's use in blade & controller. to read more click here.

 

Comments

Popular posts from this blog

Update Laravel version 5.5 to 5.8

Best practices to handle the Laravel request validation.

Write SQL queries in Laravel migration.