繁体   English   中英

(laravel)使用变量名模型的查询构建器

[英](laravel) Query builder using model of variable name

我正在尝试使用其laravel模型类插入表中,但是我需要能够插入变量指示的表中。 如何才能做到这一点? 我尝试了香草PHP方法和laravel外观方法,但都给了我错误:

$modelName = "model";

# this works no problem:
$model = new model();

# this blows up:
$model = new $modelName();

# this blows up:
$model::create($request->all());

laravel给出的错误:

FatalThrowableError in Controller.php line 44:
Class 'model' not found

您必须输入完全限定的类名称:

必须使用标准名称(带有名称空间前缀的类名称)。

$modelName = "App\\User"; // assuming User is located in the App namespace
$model = new $modelName();

您可以在此处了解更多信息。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM