Level 25
from the error you are saying seems like you haven't added HasRoles trait in you User model
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I need to insert a "super admin" user to user table during user table migration . along with that I need to assign role for user.
$userData = DB::table('users')->insert(
array('employee_id' => '1', 'name' => 'Admin', 'username' => 'admin', 'email' => '[email protected]', 'password' => Hash::make('123456'),'status' => '1'),
);
$role = Role::create(['name' =>'Admin']);
$userData->assignRole('Admin');
I am getting error call to undefined function assignRole()
I have added following in top of migration file
use Spatie\Permission\Models\Role;
use Spatie\Permission\Models\Permission;
because you used DB to create the users table entry and not an Eloquent model
you cannot therefore run any model functions on $userData
change your seeder to $userData =User::create([...])
Please or to participate in this conversation.