0

How to call dynamic Model in Laravel 4 ?

Imagine that I have this

$job = CfgJob::orderBy('Name')->get(); 

What I want to do is to pass value to call the model. Something like this

$value='Job';

$job = Cfg.$value::orderBy('Name')->get(); 

Is it possible to this on PHP and Laravel 4 ?

Brodie
  • 429
  • 1
  • 5
  • 16
  • Take a look at this answer: http://stackoverflow.com/a/3121559/2539605 – kalley Jul 10 '14 at 13:13
  • possible duplicate of [How can I call a static method on a variable class?](http://stackoverflow.com/questions/642391/how-can-i-call-a-static-method-on-a-variable-class) – Jeff Lambert Jul 10 '14 at 13:16
  • if o do this $classname = 'CfgDirector'; $dir=$classname::orderBy('Name')->get(); It says class not found. To solve i need to specify the path of model $dir="Director"; $classname = 'app\models\Cfg'.$dir; $dir=$classname::orderBy('Name')->get(); – Pedro Oliveira Jul 10 '14 at 18:25

1 Answers1

1

I think this might work (cant test it at the moment):

$value='Job';
$value= ('Cfg'.$value);

$job = $value::orderBy('Name')->get(); 
Laurence
  • 58,936
  • 21
  • 171
  • 212