-1

My user model like this :

namespace App\Models\Auth;
...
class User extends Authenticatable
{
    ...
    public function vendor()
    {
        return $this->belongsTo(Vendor::class, 'vendor_id', 'id');
    }
}

My vendor model like this :

namespace App\Models;
...
class Vendor extends Model
{
    ...
    public function users() 
    {
        return $this->hasMany(User::class, 'id', 'vendor_id');
    }
}

If the relation run, there exist error like this :

Class 'App\Models\Auth\Vendor' not found 

It seems that the error occurred because the vendor model is not in the auth folder

How do I solve that error without moving the vendor model to the auth folder?

moses toh
  • 12,344
  • 71
  • 243
  • 443

2 Answers2

1

In simple word, you need to import Vendor class into the User class.

namespace App\Models\Auth;
use App\Models\Vendor; //code to be added
...
class User extends Authenticatable
{
    ...
    public function vendor()
    {
        return $this->belongsTo(Vendor::class, 'vendor_id', 'id');
    }
}
Hakimdc R
  • 26
  • 3
1

Pretty sure it's because you didn't explicitly load the Vendor class into the User file.

Add into your User file

use App\Models\Vendor;
mconkin
  • 121
  • 7