[英]Laravel 5.4 class User not found
I am using laravel 5.4 i got and error that 我正在使用laravel 5.4我得到了错误
FatalThrowableError in HasRelationships.php line 487: Class 'User' not found
HasRelationships.php第487行中的FatalThrowableError:未找到“用户”类
In my model i am using the following code 在我的模型中,我使用以下代码
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Auth;
use App\User;
class Review extends Model
{
public function user()
{
return $this->belongsTo('User');
}
}
Could any one help me fix this error 任何人都可以帮我解决这个错误
You should use App\\User
in belongsTo
. 您应该在
belongsTo
使用App\\User
。 If you provide only User
it will look for User
in the base directory. 如果您只提供
User
,它将在基本目录中查找User
。 But User
is in the App
namespace. 但是
User
位于App
命名空间中。 : :
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Auth;
use App\User;
class Review extends Model
{
public function user()
{
return $this->belongsTo('App\User');
}
}
Edit : 编辑:
belongsTo
require a namespace of a model you can achieve it either with above mentioned method or with User::class
. belongsTo
需要模型的命名空间,您可以使用上述方法或User::class
来实现它。 As it will also return the namespace of User
class. 因为它还将返回
User
类的命名空间。
return $this->belongsTo(User::class);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.