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

satheeshkumarj's avatar

Assign Role in migration file using spaite permission pacakage

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;
0 likes
2 replies
krekas's avatar

from the error you are saying seems like you haven't added HasRoles trait in you User model

Snapey's avatar
Snapey
Best Answer
Level 122

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([...])

1 like

Please or to participate in this conversation.