Laravel – How to run cron jobs for multiple companies

  cron, laravel

I have this code in Laravel-5.8 cron jobs:

public function handle()
{
    $linemanager = User::distinct()->where('company_id', 1)->whereNotNull('line_manager_id')->pluck('line_manager_id');
    $users = User::whereIn('employee_code', $linemanager)->where('company_id', 1)->get();
    foreach ($users as $user) {
        if (! $user->hasRole('Line Manager')) {
           $user->assignRole('Line Manager');  
        }
    }        
 } 

Initially I have just one company. Each user table have company_id, that’s why company_id is 1. I use the user table to Assign Line manager role (Spatie RBAC). I did this in the cron jobs. I have multiple companies with user table still having company_id.

How do I now re-write the cron jobs (Since I have multiple companies now and can’t determine which is 1 and so on) to assign Line Manager roles to the users?

Thanks

Source: Laravel

Leave a Reply