Laravel 5 Querying with relations causes "Call to a member function addEagerConstraints() on null" error

59,919

Solution 1

You are missing return statements in the methods that define relations. They need to return relation definition.

Replace

public function roles()
{
    $this->belongsToMany('\App\Role', 'fk_user_role', 'user_id', 'role_id');
}

With

public function roles()
{
    return $this->belongsToMany('\App\Role', 'role_user', 'user_id', 'role_id');
}

Solution 2

You forgot the return in your functions

Do:

return $this->belongsToMany('\App\User', 'fk_role_user', 'role_id', 'user_id');

return $this->belongsToMany('\App\Role', 'fk_user_role', 'user_id', 'role_id');

Solution 3

You need to use Return for your function's result. If you do not do that, Laravel does not know What should do with that function without any action. Just use like this

return $this->hasOne(xxx, xx, xx);

Enjoy your coding !

Solution 4

Make sure you have written return in your model function relation.

return $this->hasMany('App\StaffShift','user_id','user_id');
Share:
59,919
Tomkarho
Author by

Tomkarho

Updated on June 08, 2021

Comments

  • Tomkarho
    Tomkarho almost 3 years

    I have been trying to create a simple user management system but keep on hitting road blocks when it comes to querying relations. For example I have users and roles and whenever I try to make a query for all users and their roles I get an error. The one in the title is only the latest one I've encountered.

    My User and Role Models look like this:

    class Role extends Model
    {
        public function users()
        {
            $this->belongsToMany('\App\User', 'fk_role_user', 'role_id', 'user_id');
        }
    }
    

    class User extends Model
    {
        public function roles()
        {
            $this->belongsToMany('\App\Role', 'fk_user_role', 'user_id', 'role_id');
        }
    }
    

    My migration table for many-to-many relationship between the two looks like this:

    public function up()
        {
            Schema::create('role_user', function (Blueprint $table) {
                $table->increments('id');
                $table->integer('user_id')->unsigned()->nullable(); //fk => users
                $table->integer('role_id')->unsigned()->nullable(); //fk => roles
    
                $table->foreign('fk_user_role')->references('id')->on('users')->onDelete('cascade');
                $table->foreign('fk_role_user')->references('id')->on('roles')->onDelete('cascade');
            });
        }
    

    And then I try to get all records with their relation in a controller:

    public function index()
    {
        $users = User::with('roles')->get();
    
        return $users;
    }
    

    So I need another pair of eyes to tell me what is it I am missing here?

  • Tomkarho
    Tomkarho almost 9 years
    Also for anyone else reading this with the same problem I needed to fix the second parameter from "fk_user_role" to "role_user".
  • Jorn
    Jorn over 7 years
    I'm going to kill myself now :P hours wasted, at least now I will never forget
  • Reem Aziz
    Reem Aziz over 6 years
    same problem as me, spend hours to solve stupid mistake thank you
  • Mr.Thanks
    Mr.Thanks about 6 years
    It's frustrating, it's just not returning
  • Istiaque Ahmed
    Istiaque Ahmed over 5 years
    may I ask you to have a look at a Laravel join related question at stackoverflow.com/questions/52149031/… ?
  • Agil
    Agil almost 5 years
    That is absolute madness :D
  • Jose Mhlanga
    Jose Mhlanga over 4 years
    This is correct. Faced this for a long time without noticing it. Relationships in eloquent are returned by the model when u call the relationship method in calling class. Thanks
  • Floris
    Floris about 4 years
    OMG this was it.
  • Ruhith Udakara
    Ruhith Udakara about 4 years
    I feel so dumb, hours wasted
  • shahsani
    shahsani over 2 years
    yes yes, this is what I forgot to write in my relation function. thanks a bunch @Leonel!
  • Bart Mommens
    Bart Mommens almost 2 years
    every single time i forget that return, <3 thanks ;)
  • Enver
    Enver almost 2 years
    That's normal, We are all doing that generally. :) You r welcome !