【问题标题】:how to change array value in Laravel Controller for detaching role under team - Laratrust如何在 Laravel Controller 中更改数组值以分离团队下的角色 - Laratrust
【发布时间】:2021-07-10 13:07:32
【问题描述】:

我将以下值从视图发送到控制器: 在视图中:

<input type="checkbox"  name="Roles[]" value="{{$user->rolesTeams[$i]->pivot}}"/>

在控制器中:

 case 'team-delete':
               $input = $request['Roles'];
                 dd($input);

结果:

array:1 [▼
  0 => "{"user_id":"15","team_id":"2","user_type":"App\\User","role_id":"2"}"
]

现在我该怎么做:

$user = user_id >> which should be 15
$team = team_id >> which whould be 2
$role = role_id >> which would be 2

所以我可以通过这个来分离团队中的角色:

$user->detachRoles($roles, $team);

【问题讨论】:

    标签: laravel view controller roles detach


    【解决方案1】:

    您可以使用 collection 或 foreach 循环数组

    collect($input)->each(function ($item)use($user){
    
             $data=json_decode($item);
                
             $roleId=$data->role_id;
             $teamId=$data->team_id;
             $user->detachRoles($roleId, $teamId);
                
          });
    

    【讨论】:

    • 没有工作,当我做 dd($data);它通过我 null;
    • @luayadnan.remove stripslashes from $data=json_decode(stripslashes($item));
    • @luayadnan 即使你可以使用 foreach 而不是 collect
    • 谢谢,我会保留本节的收集作为其非常小的操作,并将在其他页面上使用 foreach。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-24
    • 1970-01-01
    • 2011-11-03
    • 2020-03-17
    • 2014-11-12
    相关资源
    最近更新 更多