如何更改 Laravel 控制器中的数组值以分离团队下的角色 - Laratrust

how to change array value in Laravel Controller for detaching role under team - Laratrust

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

<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);

您可以使用集合或 foreach 循环数组

collect($input)->each(function ($item)use($user){

         $data=json_decode($item);
            
         $roleId=$data->role_id;
         $teamId=$data->team_id;
         $user->detachRoles($roleId, $teamId);
            
      });