Are Laravel abilities polymorphic on the argument?

Viewed 378

My understanding is that abilities defined using policies are indeed polymorphic, meaning that:

Gate::allows('update', $post);
Gate::allows('update', $comment);

will call different functions, if the two objects are of different classes, that are registered with different policies:

protected $policies = [
    Post::class => PostPolicy::class,
    Comment::class => CommentPolicy::class,
];

While it seems to me that abilities defined using $gate->define() are not polymorphic, meaning that two invocations using the same policy name will overwrite each other:

$gate->define('update', function ($user, $post)    { /* THIS IS THROWN AWAY! */ });
$gate->define('update', function ($user, $comment) { /* the last one is kept */ });

Is this correct?

Is there any relationship between the ability names shown in the documentation for the non-polymorphic examples (update-post, update-comment) and those shown in the policy examples (update)?

I mean, is the -post suffix added or inferred by Laravel? Or is it just an example?

1 Answers
Related