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

PhpMysqlLaravelLaravel 5Eloquent

Php Problem Overview


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?

Php Solutions


Solution 1 - Php

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 - Php

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 - Php

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 - Php

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

return $this->hasMany('App\StaffShift','user_id','user_id');

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTomkarhoView Question on Stackoverflow
Solution 1 - Phpjedrzej.kuryloView Answer on Stackoverflow
Solution 2 - PhpLeonel KahameniView Answer on Stackoverflow
Solution 3 - PhpEnverView Answer on Stackoverflow
Solution 4 - PhpKaleemullahView Answer on Stackoverflow