Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call Laravel model by string [duplicate]

Is it possible to call a Laravel model by string?

This is what i'm trying to achieve but its failing:

$model_name = 'User';
$model_name::where('id', $id)->first();

I get the following exception:

exception 'ErrorException' with message 'Undefined variable: User'
like image 728
Parampal Pooni Avatar asked Dec 22 '15 06:12

Parampal Pooni


4 Answers

Yes, you can do this, but you need to use the fully qualified class name:

$model_name = 'App\Model\User';
$model_name::where('id', $id)->first();

If your model name is stored in something other than a plain variable (e.g. a object attribute), you will need to use an intermediate variable in order to get this to work.

$model = $this->model_name;
$model::where('id', $id)->first();
like image 62
patricus Avatar answered Nov 12 '22 06:11

patricus


Try this:

$model_name = 'User';
$model = app("App\Model\{$model_name}");
$model->where('id', $id)->first();
like image 21
Emeka Mbah Avatar answered Nov 12 '22 07:11

Emeka Mbah


This method should work well:

private function getNamespace($model){
    $dirs = glob('../app/Models/*/*');

    return array_map(function ($dir) use ($model) {
        if (strpos($dir, $model)) {
            return ucfirst(str_replace(
                '/',
                '\\',
                str_replace(['../', '.php'], '', $dir)
            ));
        }
    }, array_filter($dirs, function ($dir) use ($model) {
        return strpos($dir, $model);
    }));
}

My project has multiple subdirectory and this function work well. So I recover the namespace of the model with $model = current($this->getNamespace($this->request->get('model'))); Then I just have to call my query: $model::all()

like image 2
itepifanio Avatar answered Nov 12 '22 06:11

itepifanio


if you use it in many of the places use this function

function convertVariableToModelName($modelName='',$nameSpace='App')
        {
            if (empty($nameSpace) || is_null($nameSpace) || $nameSpace === "") 
            {                
               $modelNameWithNameSpace = "App".'\\'.$modelName;
                return app($modelNameWithNameSpace);    
            }

            if (is_array($nameSpace)) 
            {
                $nameSpace = implode('\\', $nameSpace);
                $modelNameWithNameSpace = $nameSpace.'\\'.$modelName;
                return app($modelNameWithNameSpace);    
            }elseif (!is_array($nameSpace)) 
            {
                $modelNameWithNameSpace = $nameSpace.'\\'.$modelName;
                return app($modelNameWithNameSpace);    
            }
        }

Example if you want to get all the user

Scenario 1:

 $userModel= convertVariableToModelName('User');
  $result = $userModel::all();

Scenario 2:

if your model in in custom namespace may be App\Models

$userModel= convertVariableToModelName('User',['App','Models']);
$result = $userModel::all();

Hope it helps

like image 1
ManojKiran Appathurai Avatar answered Nov 12 '22 05:11

ManojKiran Appathurai