Я хочу получить всех пользователей и связанные роли для одной таблицы в laravel.но я не смог продолжить, потому что я получаю сообщение об ошибке
Это моя функция контроллера
public function index(){
if(Auth::user()->isAdmin==1){
$users = User::with('roles')->get();
return view('users', campact($users));
} else {
abort(403, 'Unauthorized action.');
}
}
Это функция ролей моей пользовательской модели
public function roles()
{
return $this->belongsToMany(Role::class, 'role_users');
}
Этофункция пользователя моего ролевого класса
public function users()
{
return $this->belongsToMany(User::class,'role_users');
}
Это моя таблица данных
@foreach ($users as $user)
<tr>
<td>{{ $user->id }}</td>
<td>{{ $user->name }}</td>
<td>{{ $user->email }}</td>
<td>
<form method="post">
{{ csrf_field() }}
<div class="form-group">
<select class="form-control" data-id="{{$user->id}}" id="role" name="role">
<option value="0">Select a Role</option>
@foreach ($users->roles as $role)
<option value="{{ $role->id }}">{{ $role->name }}</option>
@endforeach
</select>
</div>
</form>
</td>
<td>{{ $user->created_at }}</td>
<td>{{ $user->updated_at }}</td>
<td><div class="form-group">
<form method="post" id="userActivate">
{{ csrf_field() }}
<label>
<input type="checkbox" class="flat-red isActive" data-id="{{$user->id}}" @if ($user->isActive) checked @endif>
</label>
</form>
</div>
</td>
<td>
<form id="userDelete" method="post" >
{{ csrf_field() }}
<button type="button" id="delete" data-id="{{$user->id}}" class="btn btn-block btn-danger btn-flat">Delete</button>
</form>
</td>
</tr>
@endforeach
Пожалуйста, помогите мне решить эту проблему.