Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Aishan's avatar

Call to a member function pluck() on null

I followed the below link to instal user management and roles, I can register users and enter to the home page, I also can enter to user management page and click on show and delete buttons both work, but when I click on edit button it gives me error :

Call to a member function pluck() on null
http://127.0.0.1:8000/users/2/edit

https://www.itsolutionstuff.com/post/laravel-8-user-roles-and-permissions-tutorialexample.html

the error is right on this the line 85 : UserController.php:85

public function edit($id)
    {
        $user = User::find($id);
        $roles = Role::pluck('name','name')->all();
        $userRole = $user->roles->pluck('name','name')->all();
 
        return view('users.edit',compact('user','roles','userRole'));
    }

0 likes
14 replies
Sinnbeck's avatar

No need to call all() after pluck()

I assume this is line 85?

$userRole = $user->roles->pluck('name','name')->all();

Can you show the user model?

Aishan's avatar

sure, I removed all() but it gave me the same error @Sinnbeck

<?php

namespace App\Models;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Spatie\Permission\Models\Role;
use Spatie\Permission\Traits\HasRoles;
use Laravel\Sanctum\HasApiTokens;

class User extends Authenticatable
{
    use HasApiTokens, HasFactory, Notifiable;

    protected $table = 'users';


    /**
     * The attributes that are mass assignable.
     *
     * @var array<int, string>
     */
    protected $fillable = [
        'name',
        'email',
        'username',
        'password',
    ];

    /**
     * The attributes that should be hidden for serialization.
     *
     * @var array<int, string>
     */
    protected $hidden = [
        'password',
        'remember_token',
    ];

    /**
     * The attributes that should be cast.
     *
     * @var array<string, string>
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];

   public function getRoleNames()
    {
        return $this->belongsToMany(Role::class);

    }
   
}

Sinnbeck's avatar

@Aishan you are missing the trait

use HasApiTokens, HasFactory, Notifiable, HasRoles; 
Aishan's avatar

should I add that line (use HasApiTokens, HasFactory, Notifiable, HasRoles; )as exactly as it is ? @Sinnbeck

Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

@Aishan you already have it, but it's missing HasRoles

class User extends Authenticatable
{
    use HasApiTokens, HasFactory, Notifiable, HasRoles;
1 like
Sinnbeck's avatar

@Aishan happy to help :) please mark a best answer to set the thread as solved

1 like
Aishan's avatar

@Sinnbeck The next issue I am facing is with Roles name, it doesn’t appear on the table along with emails and names , besides , when I click on Manage Role and Manage Posts tab it gives me 403 errors referring to USER DOES NOT HAVE THE RIGHT PERMISSIONS. If you could help me for this too!

ZohaibAhmed's avatar

@Sinnbeck pay tabs is a Dubai application like stripe and PayPal you know better in laravel my question is how we can use and implement pay tabs

Sinnbeck's avatar

@ZohaibAhmed I dont see how that is related to this thread at all? If you have a question, please start a new thread/discussion

1 like

Please or to participate in this conversation.