Skip to content

Model Restrictions

Sometimes you might want to restrict an ability to a specific model type or instance.

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.

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.

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);

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
}

You can grant model-specific abilities to roles:

// Allow editors to create any post
Warden::allow('editor')->to('create', Post::class);
// Then assign the role
Warden::assign('editor')->to($user);