D
D
demeys2019-06-16 18:31:52
Laravel
demeys, 2019-06-16 18:31:52

How to get model by table name?

I pass the name of the table as a parameter.
Route::get('/{table}/sort', '[email protected]');
The models I have are in the Models folder
. For example, the User namespace App\Models\User model has this
. Is there a way to get the model by the table name?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
JhaoDa, 2019-06-16
@JhaoDa

If we follow the naming convention, then

$model = '\App\Models\\'.\ucfirst(\Illuminate\Support\Str::singular($table));
$instance = new $model();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question