php 尝试访问模型时找不到类“App\models\Test”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28431742/
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
Class 'App\models\Test' not found when try to access model
提问by Hardik Ranpariya
I am try to learn laravel framework.
我正在尝试学习 Laravel 框架。
when i am try to use model at that time i got some error.
当我当时尝试使用模型时,我遇到了一些错误。
In my test controller
在我的测试控制器中
<?php
namespace App\Http\Controllers;
use App\models\Test;
class TestController extends Controller {
public function __construct()
{
$this->middleware('guest');
}
public function editData($id) {
$result = DB::select('select * from users where id = ?', array($id));
$data['data'] = $result[0];
return view('myview', $data);
}
}
?>
and in my model
在我的模型中
model path is app/models/Test.php
模型路径是 app/models/Test.php
model name is Test.php
模型名称是 Test.php
<?php
use Illuminate\Database\Eloquent\Model;
class Test extends Model {
protected $table = 'users';
public static function getMyData(){
$user = Test::find();
}
}
?>
Output :
输出 :
FatalErrorException in TestController.php line 56:
Class 'App\models\Test' not found
I am also try this command.
我也在尝试这个命令。
composer dump-autoload
But is not working.
但不工作。
回答by lukasgeiter
Your Test
model isn't in any namespace at all. You would need to reference it by:
您的Test
模型根本不在任何命名空间中。您需要通过以下方式引用它:
use Test;
or:
或者:
\Test::getMyData();
Or just put it in the namespace you try to reference it now:
或者只是将它放在您现在尝试引用它的命名空间中:
<?php
namespace App\models;
use Illuminate\Database\Eloquent\Model;
class Test extends Model {
protected $table = 'users';
public static function getMyData(){
$user = Test::find();
}
}
?>
回答by Udhav Sarvaiya
In the model, write the below code,
在模型中,编写以下代码,
namespace App\models;
This would work , only missing namespace in your code, rest all code is perfect
这会起作用,只有代码中缺少命名空间,其余的代码都是完美的
回答by Bahri Eskander
modify config in vendor/barryvdh/laravel-ide-helper/config/ide-helper.php
修改 vendor/barryvdh/laravel-ide-helper/config/ide-helper.php 中的配置
'model_locations' => array(
'app\Models', //your models' location
),