我遵循了如何使用laravel应用程序来使用角色,权限的教程。我正在使用委托包 这个界面:
但是,我面临着2个错误,(我会在同一个主题中谈论它们,因为我认为它们可能是由同样的事情引起的):
1)Gate.php第317行中的ErrorException: isset中的非法偏移类型或空
- >当我点击“项目”或“角色”时会发生这种情况
2)UserController.php第88行中的FatalErrorException: 在null
上调用成员函数pluck()- >当我尝试编辑用户时会发生这种情况
这是Usercontroller:
//....
public function edit($id)
{
$user = User::find($id);
$roles = Role::pluck('display_name','id');
$userRole = $user->roles->pluck('id','id')->toArray();
return view('users.edit',compact('user','roles','userRole'));
}
public function update(Request $request, $id)
{
$this->validate($request, [
'name' => 'required',
'email' => 'required|email|unique:users,email,'.$id,
'password' => 'same:confirm-password',
'roles' => 'required'
]);
$input = $request->all();
if(!empty($input['password'])){
$input['password'] = Hash::make($input['password']);
}else{
$input = array_except($input,array('password'));
}
$user = User::find($id);
$user->update($input);
DB::table('role_user')->where('user_id',$id)->delete();
foreach ($request->input('roles') as $key => $value) {
$user->attachRole($value);
}
return redirect()->route('users.index')
->with('success','User updated successfully');
}
这是ItemController:
public function store(Request $request)
{
$this->validate($request, [
'title' => 'required',
'description' => 'required',
]);
Item::create($request->all());
return redirect()->route('itemCRUD2.index')
->with('success','Item created successfully');
}
public function show($id)
{
$item = Item::find($id);
return view('ItemCRUD2.show',compact('item'));
}
public function edit($id)
{
$item = Item::find($id);
return view('ItemCRUD2.edit',compact('item'));
}
这是RoleController:
public function index(Request $request)
{
$roles = Role::orderBy('id','DESC')->paginate(5);
return view('roles.index',compact('roles'))
->with('i', ($request->input('page', 1) - 1) * 5);
}
public function create()
{
$permission = Permission::get();
return view('roles.create',compact('permission'));
}
public function store(Request $request)
{
$this->validate($request, [
'name' => 'required|unique:roles,name',
'display_name' => 'required',
'description' => 'required',
'permission' => 'required',
]);
$role = new Role();
$role->name = $request->input('name');
$role->display_name = $request->input('display_name');
$role->description = $request->input('description');
$role->save();
foreach ($request->input('permission') as $key => $value) {
$role->attachPermission($value);
}
return redirect()->route('roles.index')
->with('success','Role created successfully');
}
public function show($id)
{
$role = Role::find($id);
$rolePermissions = Permission::join("permission_role","permission_role.permission_id","=","permissions.id")
->where("permission_role.role_id",$id)
->get();
return view('roles.show',compact('role','rolePermissions'));
}
public function edit($id)
{
$role = Role::find($id);
$permission = Permission::get();
$rolePermissions = DB::table("permission_role")->where("permission_role.role_id",$id)
->pluck('permission_role.permission_id','permission_role.permission_id');
return view('roles.edit',compact('role','permission','rolePermissions'));
}
模型:User.php
class User extends Authenticatable
{
use Notifiable;
protected $table = 'users';
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'password',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
}
有人可以帮我解决这个问题吗?提前谢谢。
更新:
在我的.env文件中将驱动程序更改为数组后。 CACHE_DRIVER = array我在点击项目或角色时得到这个。试着找出如何将角色归属于用户!!
答案 0 :(得分:2)
您需要先将正确的关系添加到用户模型中,然后才能加载它们。您正在尝试从roles
模型加载属性User
,但这不存在,因此返回null
。
必须将EntrustUserTrait
添加到您的User
模型中,defined here。