简体   繁体   中英

Laravel 5.4 class User not found

I am using laravel 5.4 i got and error that

FatalThrowableError in HasRelationships.php line 487: Class 'User' not found

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 . If you provide only User it will look for User in the base directory. But User is in the App namespace. :

<?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 . As it will also return the namespace of User class.

return $this->belongsTo(User::class);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM