Search code examples
laravelviewcontrollerrolesdetach

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


I sent the below value from view to controller: in View:

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

In Controller:

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

Result:

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

now How can I make this like:

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

so I can pass this to detach the Role within the team:

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

Solution

  • You can loop array using collection or foreach

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