laravel 尝试命名我的模型时找不到类“Models\Eloquent”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18553809/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 08:21:53  来源:igfitidea点击:

Class 'Models\Eloquent' not found when trying to namespace my models

phpnamespaceslaravellaravel-4composer-php

提问by Gravy

I am trying to use namespaces with my models in a Laravel 4 application.

我正在尝试在 Laravel 4 应用程序中对我的模型使用命名空间。

Models/Fix.php

模型/Fix.php

<?php 
namespace Models; // Namespace Declaration
class Fix extends Eloquent {        
    public static function getFixes() {
        return DB::table('fixes')->get();
    }
}
?>

FixController.php

修复控制器.php

<?php
class FixController extends BaseController {
    public function showFix() {
        $fixes = Models\Fix::getFixes();  // Referring to Fix Class in Models namespace  
        return View::make('partials.fix', array(
            'fixes' => $fixes,
        ));
    }
}
?>

In doing this, I receive the following error:

在这样做时,我收到以下错误:

Symfony \ Component \ Debug \ Exception \ FatalErrorException
Class 'Models\Fix' not found

$fixes = Models\Fix::getFixes();

Following https://coderwall.com/p/nhwq8wI used composer dump-autofrom terminal window and tried re-running... To find the following error.

遵循https://coderwall.com/p/nhwq8wcomposer dump-auto从终端窗口使用并尝试重新运行...找到以下错误。

Symfony \ Component \ Debug \ Exception \ FatalErrorException
Class 'Models\Eloquent' not found

class Fix extends Eloquent {

回答by Gravy

Solution - Need to add use statements into each of the models...

解决方案 - 需要在每个模型中添加 use 语句...

<?php 
namespace Models;
use Eloquent; // ******** This Line *********
use DB;       // ******** This Line *********
class Fix extends Eloquent {
    public static function getFixes() {
        return DB::table('fixes')->get();
    }
}

?>