asignRole in filament-shield

Hi everyone, I'm facing an issue with Spatie Laravel Permission. I'm trying to assign a role to a user after syncing data from another database. The user is created or updated successfully, but the assignRole method doesn't seem to work — no roles are assigned, and nothing is written to the model_has_roles table. What I've Tried: User model uses HasRoles trait. Role user exists in the roles table. Queue worker is running properly. Ran php artisan permission:cache-reset. Added logging after assignRole, but no logs appear.
4 Replies
Arlyzatun
ArlyzatunOP2w ago
<?php

namespace App\Jobs;

use App\Models\User;
use App\Models\Student;
use Illuminate\Support\Facades\DB;
use Spatie\Permission\Models\Role;
use Illuminate\Support\Facades\Log;
use Illuminate\Foundation\Queue\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;

class SyncUsersJob implements ShouldQueue
{
use Queueable;

/**
* Create a new job instance.
*/
public function __construct()
{
//
}

public function handle()
{
$students = DB::connection('master_db')
->table('students')
->where('status', 'active')
->get();

foreach ($students as $student) {
// Insert or Update user, and retrieve the instance
$user = User::updateOrCreate(
['email' => $student->email_student],
[
'name' => $student->full_name,
'password' => bcrypt($student->student_number),
'is_first' => false,
]
);

// Assign Role using Spatie
$role = Role::firstOrCreate(['name' => 'user']);
if (!$user->hasRole('user')) {
$user->assignRole($role);
Log::channel('sync')->info("Assigned role 'user' to user {$user->email}");
Log::info("Assigned role 'user' to user {$user->email}");
}
}
}
}
<?php

namespace App\Jobs;

use App\Models\User;
use App\Models\Student;
use Illuminate\Support\Facades\DB;
use Spatie\Permission\Models\Role;
use Illuminate\Support\Facades\Log;
use Illuminate\Foundation\Queue\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;

class SyncUsersJob implements ShouldQueue
{
use Queueable;

/**
* Create a new job instance.
*/
public function __construct()
{
//
}

public function handle()
{
$students = DB::connection('master_db')
->table('students')
->where('status', 'active')
->get();

foreach ($students as $student) {
// Insert or Update user, and retrieve the instance
$user = User::updateOrCreate(
['email' => $student->email_student],
[
'name' => $student->full_name,
'password' => bcrypt($student->student_number),
'is_first' => false,
]
);

// Assign Role using Spatie
$role = Role::firstOrCreate(['name' => 'user']);
if (!$user->hasRole('user')) {
$user->assignRole($role);
Log::channel('sync')->info("Assigned role 'user' to user {$user->email}");
Log::info("Assigned role 'user' to user {$user->email}");
}
}
}
}
toeknee
toeknee2w ago
Are you using teams in the permissions, if so you need to run: setPermissionsTeamId(TeamID HERE); Is the Job being run? php artisan queue:work?
Arlyzatun
ArlyzatunOP2w ago
Yes, the job running with queue:work Hmm, sorry I havent hear about "teams in permission", in my app the role is basic, so user only can have 1 role (super_admin, admin, user), I also using filament-shield so I can handle permission based on their role @toeknee
toeknee
toeknee2w ago
So the fact not logs get set... shows you hvae a bigger issue, log if the forech is ever being run firstly.

Did you find this page helpful?