在laravel 5.3中调用null上的成员函数lists()

In laravel 5.3, I am trying to edit registered users in the All Users page (admin/users) and assign roles and permissions to them. I used the codes from laravel 5.2 pdf book. but anytime i click on a user to edit it. It brings up below error

FatalThrowableError in UsersController.php line 26: Call to a member function lists() on null.

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'));
}

The lists() function has been discontinued since Laravel 5.3. You should use pluck instead.

$selectedRoles = $user->roles->pluck('id');

Regarding the error: You must not be having any roles for a particular user, hence the error.

add below code in

app/User.php

public function roles()
{
    return $this->belongsToMany('App\Role');
}

After that use bellow code in

Http/Controllers/UserController.php

$userRole = $user->roles()->lists('id','id')->toArray();

Instead of

$userRole = $user->roles->lists('id','id')->toArray();