简体   繁体   English

Laravel 5 查询关系导致“调用成员函数 addEagerConstraints() on null”错误

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

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?所以我需要另一双眼睛来告诉我我在这里错过了什么?

You are missing return statements in the methods that define relations.您在定义关系的方法中缺少return语句。 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');
}

You forgot the return in your functions您忘记了函数中的返回

Do:做:

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

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

确保您在模型函数关系中写入了 return 。

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

You need to use Return for your function's result.您需要将 Return 用于函数的结果。 If you do not do that, Laravel does not know What should do with that function without any action.如果不这样做,Laravel 不知道应该如何处理该函数而不进行任何操作。 Just use like this就这样使用

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

Enjoy your coding !享受你的编码!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 null 上调用成员函数 addEagerConstraints() 在 laravel 6 上出现此错误 - Call to a member function addEagerConstraints() on null getting this error on laravel 6 在 null 上调用成员 function addEagerConstraints() - Call to a member function addEagerConstraints() on null 在字符串上调用成员函数 addEagerConstraints() - Laravel 8 - Call to a member function addEagerConstraints() on string - Laravel 8 在float LARAVEL上调用成员函数addEagerConstraints() - Call to a member function addEagerConstraints() on float LARAVEL Laravel在布尔值上调用成员函数addEagerConstraints() - Laravel call to a member function addEagerConstraints() on boolean Laravel-在字符串上调用成员函数addEagerConstraints() - Laravel - Call to a member function addEagerConstraints() on string Laravel 调用成员 function addEagerConstraints() on float - Laravel call to a member function addEagerConstraints() on float 由于条件关系,在 null 上调用成员函数 addEagerConstraints() - Call to a member function addEagerConstraints() on null because of a conditional relationship 我在 integer 上收到对成员 function addEagerConstraints() 的错误调用 - I get The Error Call to a member function addEagerConstraints() on integer 在字符串上调用成员函数 addEagerConstraints() - Call to a member function addEagerConstraints() on string
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM