Reputation: 223
I am trying to show roles related to the user but unfortuntly not showing roles in the selection option related to the user please help me how can resolve that?
controller
public function edit(User $user)
{
$data = [
'roles' => Role::where('name', '!=', 'super-admin')->get(),
'users' => $user->load('roles'),
];
return view('cms.user_management.edit', $data);
}
html view
<select class="js-example-basic-multiple" name="roles[]"
multiple="multiple">
<option disabled >please select</option>
@foreach($roles as $key => $value)
<option value="{{ $value->name }}" {{ $users->id ==
$value->id ? 'selected="selected"' : '' }}>{{ $value->name }}</option>
@endforeach
</select>
Upvotes: 0
Views: 1801
Reputation: 2567
Just change you code as below to retriew all roles that assigned to a user
'roles' => $user->getRoleNames();
$user->getRoleNames()
will return array of roles assigned to that user like ['role01','role02']
So then you just need to loop that array inside blade and make required conditions .
Find more regarding their basic usages
Upvotes: 1