Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 当模型名存储在变量中时,有没有办法通过变量动态访问laravel模型?_Php_Laravel - Fatal编程技术网

Php 当模型名存储在变量中时,有没有办法通过变量动态访问laravel模型?

Php 当模型名存储在变量中时,有没有办法通过变量动态访问laravel模型?,php,laravel,Php,Laravel,基本上我需要做的是: $x = 'Admin'; $model = new \ReflectionClass($x); $model->getFieldList(); 我在应用程序文件夹中有管理模型。 显然,这是行不通的。有人知道吗?可以这样做吗?首先需要将名称空间添加到模型中。默认情况下,这是App\。因此,您的字符串必须是\App\Admin。现在,您可以使用这个字符串简单地创建一个类实例 $x = '\\App\\Admin'; $model = new $x(); 首先,需要将

基本上我需要做的是:

$x = 'Admin';
$model = new \ReflectionClass($x);
$model->getFieldList();
我在应用程序文件夹中有管理模型。
显然,这是行不通的。有人知道吗?可以这样做吗?

首先需要将名称空间添加到模型中。默认情况下,这是App\。因此,您的字符串必须是\App\Admin。现在,您可以使用这个字符串简单地创建一个类实例

$x = '\\App\\Admin';
$model = new $x();

首先,需要将名称空间添加到模型中。默认情况下,这是App\。因此,您的字符串必须是\App\Admin。现在,您可以使用这个字符串简单地创建一个类实例

$x = '\\App\\Admin';
$model = new $x();

可以这样做,但需要使用完全限定的类名。例如,我的模型位于模型目录中:

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

可以这样做,但需要使用完全限定的类名。例如,我的模型位于模型目录中:

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

我在控制器构造函数中使用类似的东西。以下是我使用的:

1首先在父控制器中声明一个变量,例如: $route\u model\u名称和$model\u位置

然后将其添加到每个子控制器:

function __construct() {
  parent::__construct();
  $this->setRouteModelName( 'model_route_name' );
  $this->setModelLocation( 'App\Models\ModelName' );
}
 public function edit( $id ) {
     return $this->getModelClass()::where( 'id', $id )->first();
 }
然后在父控制器中添加一个方法来初始化模型类,以便准备进行查询:

 /**
  * @return mixed
  */
 protected function getModelClass() {
     return app( $this->model_class );
 }
然后,您可以更改每个子控制器中的模型位置和布线,并且仍然在父控制器中使用相同的方法:

function __construct() {
  parent::__construct();
  $this->setRouteModelName( 'model_route_name' );
  $this->setModelLocation( 'App\Models\ModelName' );
}
 public function edit( $id ) {
     return $this->getModelClass()::where( 'id', $id )->first();
 }

我在控制器构造函数中使用类似的东西。以下是我使用的:

1首先在父控制器中声明一个变量,例如: $route\u model\u名称和$model\u位置

然后将其添加到每个子控制器:

function __construct() {
  parent::__construct();
  $this->setRouteModelName( 'model_route_name' );
  $this->setModelLocation( 'App\Models\ModelName' );
}
 public function edit( $id ) {
     return $this->getModelClass()::where( 'id', $id )->first();
 }
然后在父控制器中添加一个方法来初始化模型类,以便准备进行查询:

 /**
  * @return mixed
  */
 protected function getModelClass() {
     return app( $this->model_class );
 }
然后,您可以更改每个子控制器中的模型位置和布线,并且仍然在父控制器中使用相同的方法:

function __construct() {
  parent::__construct();
  $this->setRouteModelName( 'model_route_name' );
  $this->setModelLocation( 'App\Models\ModelName' );
}
 public function edit( $id ) {
     return $this->getModelClass()::where( 'id', $id )->first();
 }

什么不起作用?你有错误吗?什么不起作用?你有错误吗?