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?
CodePudding user response:
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()]);
CodePudding user response:
We can just use call it putting a full path of the model. for eg:
public function callModel(Request $request){
$create = App\Models\$model::create($request->input());
dd($create);
}