4

I am trying to call a Model dynamically, but I am getting ..

Class 'xxx' not found

Here's my code:

use App\xxx;
.
.
.

$model = ucfirst(explode('_', $title)[0]);
$model::where('name', 'john')->get()->toArray();

dd($model) gives:

"xxx"
CairoCoder
  • 3,091
  • 11
  • 46
  • 68

1 Answers1

13

If $xxx is not full class name, do this:

$model = 'App\\' . $model;
$model::where('name', 'john')->get()->toArray();
CairoCoder
  • 3,091
  • 11
  • 46
  • 68
Alexey Mezenin
  • 158,981
  • 26
  • 290
  • 279