Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call laravel model dynamically [duplicate]

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"
like image 562
CairoCoder Avatar asked Dec 19 '22 01:12

CairoCoder


1 Answers

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

$model = 'App\\' . $model;
$model::where('name', 'john')->get()->toArray();
like image 158
Alexey Mezenin Avatar answered Dec 20 '22 16:12

Alexey Mezenin