class ContactPolicy {
public function createAny($user) {
return $user->canCreateContacts();
}
Example 9-28. Checking authorization against a policy
// Gate
if (Gate::denies('update', $contact)) {
abort(403);
}
// Gate if you don't have an explicit instance
if (! Gate::check('create', Contact::class)) {
abort(403);
}
// User
if ($user->can('update', $contact)) {
// Do stuff
}
// Blade
@can('update', $contact)
// Show stuff
@endcan
if (policy($contact)->update($user, $contact)) {
// Do stuff
}
Example 9-29. Overriding policies with the before() method
public function before($user, $ability) {
if ($user->isAdmin()) {
return true;
}
}