dtm37893 2016-09-08 02:20
浏览 58
已采纳

向一个角色添加多个权限委托Laravel 5的基于角色的权限


I just add entrust to Laravel project, i can create role, create permission , attachPermission, assignRole.

Now i want to attach multiple permissions to one Role ,for example i add this permissions [create-user,edit-user,remove-user,update-user] to superAdmin Role.

public function attachPermission(Request $request){
        $role = Role::where('name', '=', $request->input('role'))->first();
        $permission = Permission::where('name', '=', $request->input('name'))->first();
        foreach ($permission as $pers){
            $role->attachPermissions($pers);
        }
            return response()->json("done");
    }

This code take last just permission, this is a backend i test with Postman.
So what's the best why to do it ? and thanks for help .

  • 写回答

1条回答 默认 最新

  • duanguoyin7008 2016-09-08 06:06
    关注

    First retrieve the permissions you wish to assign to the role:

    $permissionNames = ['name1', 'name2', 'name3'];
    
    $permissions = Permission::whereIn('name', $permissionNames)->get();
    

    and then you can pass the $permissions collection to the attachPermissions() method which accepts an array of Eloquent models:

    $role->attachPermissions($permissions);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部