加入关系给出模型未找到错误

Join relation giving the model not found error

我正在尝试检索评论和该评论的用户。我在用户和评论之间有以下关系。

这就是我正在尝试的

$users = Comment::with('user')->get();

但我得到

Class 'User' not found

我不确定我的代码有什么问题。

感谢任何帮助。

评论

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use App\User;
use App\Event;

class Comment extends Model
{
        // Table Name
    protected $table = 'comments';
        //primary key
    public $primaryKey = 'id';

    protected $fillable = ['user_id', 'event_id', 'comment', 'deleted_at'];

    public function user()
    {
        return $this->belongsTo('User');
    }

用户

<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Tymon\JWTAuth\Contracts\JWTSubject;

class User extends Authenticatable implements JWTSubject
{
    use Notifiable;

    protected $fillable = [
        'name', 'email', 'password',
    ];


    protected $hidden = [
        'password', 'remember_token',
    ];


    public function getJWTIdentifier()
    {
        return $this->getKey();
    }


    public function getJWTCustomClaims()
    {
        return [];
    }

    public function setPasswordAttribute($value) 
    {
        return $this->attributes['password'] = bcrypt($value);
    }
}

改变

public function user()
{
    return $this->belongsTo('User');
}

public function user()
{
    return $this->belongsTo('App\User');
}

文档:https://laravel.com/docs/5.7/eloquent-relationships#updating-belongs-to-relationships

你可以这样使用

 public function user()
{
    return $this->belongsTo('App\User', 'user_id');
}

public function user()
    {
        return $this->belongsTo('App\User');
    }

如果使用这个则不需要定义use App\User;

希望有用。

谢谢