I have user and role two table data,I've fetch the data using eloquent and send to my view. Now, I want to populate user role checkbox dynamically. at this time which is I've written as hard code
Here,is my code.
@foreach($users as $user)
<tr>
<td>{{$user->name}}</td>
<td>{{$user->email}}</td>
@foreach($user->roles as $role)
<td><input type="checkbox" name="" {{$user->hasRole('User') ? 'checked' : ''}}></td>
<td><input type="checkbox" name="" {{$user->hasRole('Admin') ? 'checked' : ''}}></td>
<td><input type="checkbox" name="" {{$user->hasRole('Author') ? 'checked' : ''}}></td>
@endforeach
<td></td>
</tr>
@endforeach
My Eloquent Query.
$users=User::with('roles')->get();
return view('admin',compact('users'));
User Model Relationship.
public function roles()
{
return $this->belongsToMany('App\Role');
}