php - Laravel choosing a class at runtime -
i trying merge 2 websites created using laravel 5 1 multisite (yes, wasn't experienced when making decision). 2 websites 1 cats , 1 dogs.
my problem have model called item, 1 in cats storing things in different table model item in dogs.
what have done in controller:
protected $posts_class; public function __construct() { $this->items_class = "app\\models\\" . config('domain') . "\\item"; } public function index() { $items = $this->items_class::all(); return view('items')->with('items', $items); } but keeps giving error:
syntax error, unexpected '::' (t_paamayim_nekudotayim)
however if do:
public function index() { $class= $this->items_class; $items = $class::all(); } it works.. don't want variables within controller method.
i know why first 1 doesn't work. if has recommendations on how make multisite work in better way 1 open suggestions. thank you.
the t_paamayim_nekudotayim operator more commonly known scope resolution operator. in context of php, used statically access class methods , variables.
the all() method static method on eloquent class model inherits from. such, should called classname::all().
if understand trying correctly, trying use dynamic variable class name. unfortunately, using $this->somevariable::all() doesn't quite work way 1 expect that, , know, have separate individual variable first.
in spirit of answering question directly way call without creating separate variable, answer use forgotten forward_static_call method.
$items = forward_static_call([$this->items_class, 'all']); if need call static method using methodology , want pass array of parameters, there related function forward_static_call_array().
reference:
http://php.net/manual/en/language.oop5.paamayim-nekudotayim.php
http://php.net/manual/en/function.forward-static-call.php
http://php.net/manual/en/function.forward-static-call-array.php
Comments
Post a Comment