使用未定义的常量 App - 在 Laravel 中假定为“App”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20252012/
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:42:28  来源:igfitidea点击:

Use of undefined constant App - assumed 'App' in Laravel

phplaravel

提问by Naing Lin Aung

I am trying to create application with laravel 4. I created Answer Model with the following code.

我正在尝试使用 laravel 4 创建应用程序。我使用以下代码创建了应答模型。

<?php namespace App\Models;

class Answer extends Eloquent {
    protected $guarded = array();

    public static $rules = array();
}

and I already seeded with sample data. But while I tried with command line in the following;

我已经播种了样本数据。但是当我在下面尝试使用命令行时;

php artisan tink
echo App\Models\Answer::all();

It showed

这显示了

Use of undefined constant App - assumed 'App'

Instead of sample data with JSON format. Please find me a solution for this.

而不是 JSON 格式的示例数据。请为我找到解决方案。

回答by The Alpha

According to your namespace your Answermodel should be in app/models/app/modelsand to extend the class you should use

根据您的命名空间,您的Answer模型应该在app/models/app/models并扩展您应该使用的类

class Answer extends \Eloquent

instead of

代替

class Answer extends Eloquent

But, I think you can get rid of this namespace, if your Answermodel is inside app/modelsfolder of you application's root folder then you don't need to use this namespace. By default "app/models"is provided in to autoloadsection of the composer.jsonfile and Laravelwill load models from this location and if you want to use any namespace in your model then create directories according to namespace and put your model in that directory under app/model, so, if your namespace for a model is Project/entitythen, your model should be in app/models/project/entityfolder and when you use your modelyou may use it like

但是,我认为您可以摆脱 this namespace,如果您的Answer模型位于app/models应用程序根文件夹的文件夹内,那么您就不需要使用 this namespace。默认情况下"app/models"提供到文件的autoload部分,并将从此位置加载模型,如果您想在模型中使用任何命名空间,则根据命名空间创建目录并将模型放在该目录下,因此,如果您的命名空间为模型然后,您的模型应该在文件夹中,当您使用您的模型时,您可以像这样使用它composer.jsonLaravelapp/modelProject/entityapp/models/project/entitymodel

project\entity\YourModel