Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Čamo's avatar
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.

0 likes
5 replies
brainbarett's avatar

Maybe you have a typo in your file names. I tested your code out and it works fine.

Čamo's avatar
Level 3

So the problem was in missing softDeletes trait in User model. Its weird cause it was automatically installed with migrations from Laravel UI. It seems like a bug.

Čamo's avatar
Level 3

Its my bad. Laravel UI does not have softDeletes on user model. I copy it from another project and made this mistake.

Please or to participate in this conversation.