0
votes

I'm trying to import the model in function and call create function of the specific model. I know which model to call from Route::currentRoute();, so it must look like

$model = Route::currentRouteName() // return like 'Blog/Post'

$create = new App\Models\$model::create($request->input());

Any ideas on how to do it in that way?

1

1 Answers

1
votes

You must put the full model path into the string, then it will work.
Also make sure that you replace forward slashes with backslashes.
And if you use 'create' you don't need 'new'.

Here the final result:

$model = str('Blog/Post')->replace('/', '\\');
$fullPath = 'App\Models\\' . $model;
$create = $fullPath::create($request->input()]);