我跟着一個關於如何使用角色,權限...與laravel應用程序的教程。我使用的委託包 這個接口:如何修復使用委託包與laravel後發生的錯誤
但是,我面對2個錯誤,(我會談談兩者的相同的話題,因爲我想也許他們造成同樣的事情) :
1)ErrorException在Gate.php行317: 非法偏移類型isset或空
- >發生這種情況時,我點擊 「項目」 或 「角色」
2)FatalErrorException在UserController.php線88:)在空 調用一個成員函數採摘(
- >當我嘗試編輯用戶
這是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 =數組我點擊項目或角色時得到這個。試圖找出如何給用戶分配角色!
'UserController'中的第88行在哪裏? – Jerodev
@Jerodev這是它:$ userRole = $ user-> roles-> pluck('id','id') - > toArray(); – Naj
您可以爲您添加用戶模型的代碼嗎? – Jerodev