[英]Call to a member function lists() on null in laravel 5.3
在laravel 5.3中,我試圖在“所有用戶”頁面(管理員/用戶)中編輯注冊用戶,並為其分配角色和權限。 我使用了laravel 5.2 pdf書中的代碼。 但是任何時候我單擊用戶進行編輯。 它顯示以下錯誤
UsersController.php第26行中的FatalThrowableError:在null上調用成員函數list()。
public function edit($id)
{
$user = User::whereId($id)->firstOrFail();
$roles = Role::all();
$selectedRoles = $user->roles->lists('id')->toArray();
return view('backend.users.edit', compact('user', 'roles', 'selectedRoles'));
}
自Laravel 5.3起, lists()
函數已終止。 您應該改用pluck
。
$selectedRoles = $user->roles->pluck('id');
關於錯誤:您一定不能對特定用戶具有任何角色,因此會出現錯誤。
在下面添加代碼
應用程序/ user.php的
public function roles()
{
return $this->belongsToMany('App\Role');
}
之后,在下面使用以下代碼
HTTP /控制器/ UserController.php
$userRole = $user->roles()->lists('id','id')->toArray();
代替
$userRole = $user->roles->lists('id','id')->toArray();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.