Gates

Defining Gates to control access

Gates are closures or classes that determine if a user is authorized to perform an action.


Difference from Policies:


  • Gates: For general actions
  • Policies: For specific resources

Examples

Defining Gate

Gate::define('update-post', function (User $user, Post $post) {
    return $user->id === $post->user_id;
});

Defining a gate for updating post.

Using Gate

if (Gate::allows('update-post', $post)) {
    //
}

if (Gate::denies('update-post', $post)) {
    //
}

Checking access with gate.