如何从数组 eloquent laravel 中获取权限名称值

Posted

技术标签:

【中文标题】如何从数组 eloquent laravel 中获取权限名称值【英文标题】:How to get permission name value from array eloquent laravel 【发布时间】:2020-12-12 02:59:45 【问题描述】:

如果我的英语有点奇怪,我很抱歉,如果你不明白我的问题,请问我。 所以,我想要这样的逻辑。当我尝试使用像下面这样的简单数组时,它可以工作。

    $a = array('car', 'bicycle', 'plane');
    $b = array('car', 'boat', 'truck');
    foreach ($a as $c)
    
        if (in_array($c,$b))
        
            echo "<b>" . $c . "</b>";
            echo "\r\n";
         else
        
            echo $c;
            echo "\r\n";
        
    

但是当我尝试在数组 laravel eloqeunt 中应用它时,看起来我无法从数组中获取值。我想匹配数据库中的数组(权限名称)。我想显示“权限”表中的所有权限名称,然后如果“权限名称”已经应用于“角色”,我希望它显示为选中状态。这是我的代码:

    $roles = Role::where('id', '=', 2)->with(['permissions'])->get();
    $permissions = Permission::all();
    foreach ($roles as $role)
    
        foreach ($permissions as $permission)
        
            $roleper = array($role['permissions']);
            $pername = array($permissions);
            if (in_array($pername,$roleper))
            
                echo "<b>" . $permission['name'] . "</b>";
                echo "\r\n";
             else 
                echo $permission['name'];
                echo "\r\n";
            

        
    

当我返回 $roleper 时,结果如下:

[["id":1,"name":"Add Color","guard_name":"web","created_at":"2020-08-10 02:49:25","updated_at":"2020-08-10 02:49:25","pivot":"role_id":2,"permission_id":1,"id":2,"name":"Edit Color","guard_name":"web","created_at":"2020-08-10 02:50:49","updated_at":"2020-08-10 02:50:49","pivot":"role_id":2,"permission_id":2,"id":3,"name":"Delete Color","guard_name":"web","created_at":"2020-08-10 02:50:57","updated_at":"2020-08-10 02:50:57","pivot":"role_id":2,"permission_id":3]]

当我返回 $pername 时,结果如下:

[["id":1,"name":"Add Color","guard_name":"web","created_at":"2020-08-10 02:49:25","updated_at":"2020-08-10 02:49:25","id":2,"name":"Edit Color","guard_name":"web","created_at":"2020-08-10 02:50:49","updated_at":"2020-08-10 02:50:49","id":3,"name":"Delete Color","guard_name":"web","created_at":"2020-08-10 02:50:57","updated_at":"2020-08-10 02:50:57","id":4,"name":"Add Material","guard_name":"web","created_at":"2020-08-10 03:11:16","updated_at":"2020-08-10 03:11:16","id":5,"name":"Edit Material","guard_name":"web","created_at":"2020-08-10 03:11:25","updated_at":"2020-08-10 03:11:25","id":6,"name":"Delete Material","guard_name":"web","created_at":"2020-08-10 03:11:36","updated_at":"2020-08-10 03:11:36","id":7,"name":"Add Brand","guard_name":"web","created_at":"2020-08-10 03:11:48","updated_at":"2020-08-10 03:11:48","id":8,"name":"Edit Brand","guard_name":"web","created_at":"2020-08-10 03:11:54","updated_at":"2020-08-10 03:11:54","id":9,"name":"Delete Brand","guard_name":"web","created_at":"2020-08-10 03:12:00","updated_at":"2020-08-10 03:12:00","id":10,"name":"Add Product","guard_name":"web","created_at":"2020-08-10 03:12:09","updated_at":"2020-08-10 03:12:09","id":11,"name":"Edit Product","guard_name":"web","created_at":"2020-08-10 03:12:17","updated_at":"2020-08-10 03:12:17","id":12,"name":"Delete Product","guard_name":"web","created_at":"2020-08-10 03:12:23","updated_at":"2020-08-10 03:12:23"]]

【问题讨论】:

【参考方案1】:

您可以使用内置方法array_intersect、array_diff 来获取两个数组之间的共同和不同元素

$permissions = Permission::all()->pluck('name')->toArray();
foreach ($roles as $role)

    $rolePermissions = $role['permissions']->pluck('name')->toArray();
    $maches = array_intersect($permissions, $rolePermissions);
    $differences = array_diff($permissions, $rolePermissions);
    //... loop $matches and $differences

【讨论】:

以上是关于如何从数组 eloquent laravel 中获取权限名称值的主要内容,如果未能解决你的问题,请参考以下文章

重新排列数组索引 Eloquent Laravel

如何在 Laravel Eloquent 中获取包含数组的 JSON 对象的值?

如何使用 Laravel Eloquent 在数据库中保存 JSON 响应,我收到错误“数组到字符串转换”

如何从laravel中的数组中获取键名

如何从 Laravel 8 Eloquent 中的嵌套关​​系中获取数据?

Laravel Eloquent - 使用 JSON 对象数组