I have just started using Laravel queues and don't know if this can't be done in Laravel
Instead of doing this which works...
$test1 = Test1::where('fieldname', $value)->exists();
$test2 = Test2::where('fieldname', $value)->exists();
$test3 = Test3::where('fieldname', $value)->exists();
$test4 = Test4::where('fieldname', $value)->exists();
I want to create an array of model classes and fieldnames then loop over the array...
$classes = [
'Test1' => 'fieldname',
'Test2' => 'fieldname',
'Test3' => 'fieldname',
'Test4' => 'fieldname',
];
foreach($classes as $class => $field) {
$class::where($field, $groupid)->exists();
}
but when I run my job I get the following message and it dies
Class 'Test1' not found
I want to do other stuff inside the loop which reduces my code down a lot and looks more readable.
Can you not call a class using a variable like I tried?
Here is my code
<?php
namespace App\Jobs;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use \App\Models\Test;
class ImportProductGroupCrm implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $product;
public $tries = 1;
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
$classes = [
'Test' => 'field-Name',
];
foreach($classes as $class => $field) {
$class::where($field, $groupid)->exists();
}
}
}