Model Restrictions
Sometimes you might want to restrict an ability to a specific model type or instance.
Restricting to a Model Class
Section titled “Restricting to a Model Class”Pass the model name as a second argument to restrict an ability to all instances of that model type:
Warden::allow($user)->to('edit', Post::class);This allows the user to edit any Post model.
Restricting to a Specific Instance
Section titled “Restricting to a Specific Instance”If you want to restrict the ability to a specific model instance, pass in the actual model instead:
Warden::allow($user)->to('edit', $post);This allows the user to edit only that specific $post instance.
Examples
Section titled “Examples”Allow a user to view all documents:
Warden::allow($user)->to('view', Document::class);Allow a user to delete a specific post:
Warden::allow($user)->to('delete', $post);Allow a role to create posts:
Warden::allow('editor')->to('create', Post::class);Checking Model-Specific Abilities
Section titled “Checking Model-Specific Abilities”When checking at the gate, pass the model or model class:
// Check if user can edit any post$boolean = Warden::can('edit', Post::class);
// Check if user can edit a specific post$boolean = Warden::can('edit', $post);On the user model:
if ($user->can('edit', $post)) { // User can edit this specific post}Combining with Roles
Section titled “Combining with Roles”You can grant model-specific abilities to roles:
// Allow editors to create any postWarden::allow('editor')->to('create', Post::class);
// Then assign the roleWarden::assign('editor')->to($user);