bishal123's avatar

Call to undefined method App\Models\User::assignRole()

I keep getting this error eventhough I have used Spatie libraries in user's model BadMethodCallException

Call to undefined method App\Models\User::assignRole()

at E:\xampp\htdocs\authentication2\vendor\laravel\framework\src\Illuminate\Support\Traits\ForwardsCalls.php:71 67▕ * @throws \BadMethodCallException 68▕ */ 69▕ protected static function throwBadMethodCallException($method) 70▕ { ➜ 71▕ throw new BadMethodCallException(sprintf( 72▕ 'Call to undefined method %s::%s()', static::class, $method 73▕ )); 74▕ } 75▕ }

• Bad Method Call: Did you mean App\Models\User::asJson() ?

1 E:\xampp\htdocs\authentication2\vendor\laravel\framework\src\Illuminate\Support\Traits\ForwardsCalls.php:36 Illuminate\Database\Eloquent\Model::throwBadMethodCallException("assignRole")

2 E:\xampp\htdocs\authentication2\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php:2132 Illuminate\Database\Eloquent\Model::forwardCallTo(Object(Illuminate\Database\Eloquent\Builder), "assignRole")

0 likes
12 replies
Sinnbeck's avatar

Did you add the HasRoles trait to the User model?

4 likes
bishal123's avatar

@Sinnbeck @MohamedTammam user.php

<?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 Laravel\Sanctum\HasApiTokens;
use Spatie\Permission\Traits\HasRoles;


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 assignRole()
   // {
   //     echo ('hello');
   // }

   public function setPasswordAttribute($value)
   {
       $this->attributes['password'] = bcrypt('$value');
   }
}
<?php

namespace Database\Seeders;

use App\Models\User;
use Illuminate\Database\Seeder;
use Spatie\Permission\Models\Role;
use Spatie\Permission\Models\Permission;


class CreateAdminUserSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        $user = User::create([
            'name' => 'Admin',
            'email' => '[email protected]',
            'username' => 'admin',
            'password' => bcrypt('admin123'),
        ]);

        $role = Role::create(['name' => 'admin']);

        $permissions = Permission::pluck('id', 'id')->all();


        $role->syncPermissions($permissions);

        $user->assignRole([$role->id]);
    }
}

MohamedTammam's avatar

Please show the code that throw the error, and also make sure that you added Spatie\Permission\Traits\HasRoles trait to your model as @sinnbeck mentioned.

bishal123's avatar

@MohamedTammam user.php

<?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 Laravel\Sanctum\HasApiTokens;
use Spatie\Permission\Traits\HasRoles;


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 assignRole()
   // {
   //     echo ('hello');
   // }

   public function setPasswordAttribute($value)
   {
       $this->attributes['password'] = bcrypt('$value');
   }
}
<?php

namespace Database\Seeders;

use App\Models\User;
use Illuminate\Database\Seeder;
use Spatie\Permission\Models\Role;
use Spatie\Permission\Models\Permission;


class CreateAdminUserSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        $user = User::create([
            'name' => 'Admin',
            'email' => '[email protected]',
            'username' => 'admin',
            'password' => bcrypt('admin123'),
        ]);

        $role = Role::create(['name' => 'admin']);

        $permissions = Permission::pluck('id', 'id')->all();


        $role->syncPermissions($permissions);

        $user->assignRole([$role->id]);
    }
}

MohamedTammam's avatar
Level 51

@bishal123 Your model should look like

class User extends Authenticatable
{
   use HasApiTokens, HasFactory, Notifiable, HasRoles;
	// ...

You need to HasRoles inside the model itself.

1 like

Please or to participate in this conversation.