title: Laravel call a model class using a variable as its name
body: <p>I have just started using Laravel queues and don't know if this can't be done in Laravel</p>
  
  <p>Instead of doing this which works...</p>
  
  <pre><code>$test1 = Test1::where('fieldname', $value)-&gt;exists();
  $test2 = Test2::where('fieldname', $value)-&gt;exists();
  $test3 = Test3::where('fieldname', $value)-&gt;exists();
  $test4 = Test4::where('fieldname', $value)-&gt;exists();
  </code></pre>
  
  <p>I want to create an array of model classes and fieldnames then loop over the array...</p>
  
  <pre><code>$classes = [
      'Test1' =&gt; 'fieldname',
      'Test2' =&gt; 'fieldname',
      'Test3' =&gt; 'fieldname',
      'Test4' =&gt; 'fieldname',
  ];
  
  foreach($classes as $class =&gt; $field) {
      $class::where($field, $groupid)-&gt;exists();
  }
  </code></pre>
  
  <p>but when I run my job I get the following message and it dies</p>
  
  <blockquote>
    <p>Class 'Test1' not found</p>
  </blockquote>
  
  <p>I want to do other stuff inside the loop which reduces my code down a lot and looks more readable.</p>
  
  <p>Can you not call a class using a variable like I tried?</p>
  
  <p>Here is my code</p>
  
  <pre><code>&lt;?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' =&gt; 'field-Name',
          ];
  
          foreach($classes as $class =&gt; $field) {
              $class::where($field, $groupid)-&gt;exists();
          }
      }
  }
  </code></pre>