【问题标题】:How can I fix class model not found error when using model name via variable?通过变量使用模型名称时,如何修复类模型未找到错误?
【发布时间】:2017-05-20 20:53:14
【问题描述】:

当我在 Laravel 5.2 中使用此代码时:

use 'App\modelname';

public function test()
{
    $model = 'modelname';
    $db = $model::find(1);
    echo $db->id;
}

它不起作用,我收到此错误:

FatalThrowableError in HomeController.php line 541:
Class 'modelname' not found? 

我该如何解决?

【问题讨论】:

  • 您检查/更新了自动装载机吗?你确定它在正确的命名空间中吗?

标签: php model laravel-5.2


【解决方案1】:

我找到了答案:

public function test()
{
    $sh = "modelname";
    $model = "App\\$sh";
    $db = $model::find(1);
    echo $db->id;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-04-14
    • 1970-01-01
    • 1970-01-01
    • 2016-10-05
    • 2019-05-23
    • 1970-01-01
    • 2022-12-06
    • 2019-03-18
    相关资源
    最近更新 更多