From the course: Advanced Laravel
Unlock the full course today
Join today to access over 24,800 courses taught by industry experts.
Write policies - Laravel Tutorial
From the course: Advanced Laravel
Write policies
- [Instructor] A while ago, we implemented the feature of canceling a ScheduledClass. Before actually deleting the model, we checked if the authorized user is the one deleting it. This ensures that the instructor can cancel only his or her class and not another user's. Instead of checking it this way, let's write a policy for the same. The first step is to create a policy for the ScheduledClass model, sail artisan make:Policy ScheduledClassPolicy. This allows us to organize all related authorization rules in one place. We have the Policy class. Next we need to register it, but Laderman is capable of automatically discovering policies if you follow the standard naming convention; that is, the policy name must match the name of the model ScheduledClass and the suffix should be Policy. Also, this class must be within the Policies directory within app. As long as you follow these conventions, you don't have to…