Laravel Policies - How to Pass Multiple Arguments to function

巧了我就是萌 提交于 2019-12-02 19:27:23

I think there is possibly some confusion here on what functions are doing what.

When you use

Gate::define('delete-comment', function ($user, $post, $comment) {
    //
});

Or in the CommentPolicy

public function delete(User $user, Post $post, Comment $comment)
{
    return $user->id === $post->user_id;
}

All you are doing is defining the rules. At this point, we aren't worried about passing anything, only that the objects we received can or should be able to interact with each other. The only difference between these two is when using policies, it's just an easy way to abstract all your rules into one simple and easy to read class. If you have an app with potentially hundreds of tables and models, it will get confusing fast if you have these rules littered all over your app so policies would help to keep them all organized.

It's when you are actually checking if someone has permission to do something when you should be passing these items along. For example, when you do the following,

if (Gate::allows('delete-comment', [$post, $comment])) {
    // 
}

Or if in the CommentController

$this->authorize('delete', [$post, $comment]);

That is what controls which parameters are going to be passed to the policy or the Gate::define method. According to the docs, the $user parameter is already added for you so in this case, you only need to worry about passing the correct $post and $comment being modified.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!