Maybe you have a typo in your file names. I tested your code out and it works fine.
Jul 30, 2021
5
Level 3
Call to undefined relationship [user] on model [App\Models\Entities\Article].
I have two simple models. App\Models\Entities\Article and App\Models\User
This is copy of Article
<?php
namespace App\Models\Entities;
use App\Models\User;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Article extends Model
{
use SoftDeletes;
protected $with = ['user'];
public function user()
{
return $this->belongsTo(User::class)->withTrashed();
}
}
This is copy of User
<?php
namespace App\Models;
use App\Models\Entities\Article;
use App\Models\Entities\Role;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
class User extends Authenticatable
{
use HasFactory, SoftDeletes, Notifiable;
protected $hidden = [
'password',
'remember_token',
];
protected $with = [
'roles'
];
public function roles()
{
return $this->belongsToMany(Role::class, 'users_roles');
}
public function articles()
{
return $this->hasMany(Article::class);
}
}
As you can see Article has method user() and User has method articles(). Ide can easily find the relation. But Laravel throws me this weird error:
Illuminate\Database\Eloquent\RelationNotFoundException Call to undefined relationship [user] on model [App\Models\Entities\Article].
Dont understand it. I cleared the cache and config. Now I really dont know what happened there.
Does somebody know what could be the problem? Thanks a lot.
Please or to participate in this conversation.