Laravel 8: 在空的多对多关系中使用 pivot 时出错

Laravel 8: Error when using pivot in an empty many to many relationship

我有以下 tables:

表格

用户

组织

organisation_members

人际关系

Eloquent 型号

模型如下:

Class User {
   public function organisationMembers(): HasMany
   {
        return $this->hasMany(OrganisationMember::class);
   }

   public function organisations(): BelongsToMany
   {
        return $this->belongsToMany(Organisation::class, 'organisation_members')
            ->withPivot('role');;
   }
}
class Organisation {
    public function organisationMembers(): HasMany
    {
        return $this->hasMany(OrganisationMember::class);
    }

    public function users(): BelongsToMany
    {
        return $this->belongsToMany(Organisation::class, 'organisation_members')
            ->using(OrganisationMember::class);
    }
}
class OrganisationMember {
    public function organisation(): BelongsTo
    {
        return $this->belongsTo(Organisation::class)->withTrashed();
    }

    public function user(): BelongsTo
    {
        return $this->belongsTo(User::class);
    }
}

问题

对于以下代码:

$organisations = User::find($id)->organisations

当用户有组织时,它按预期工作。

但是,当我对没有组织的用户执行此方法时,我得到的不是空集合,而是以下内容:

Illuminate\Database\QueryException with message 'SQLSTATE[42S22]: Column not found: 1054 Unknown column 'organisation_members.role' in 'field list'
(SQL: select `organisations`.*,
`organisation_members`.`user_id` as `pivot_user_id`,
`organisation_members`.`organisation_id` as `pivot_organisation_id`,
`organisation_members`.`role` as `pivot_role` from `organisations` inner join
`organisation_members` on `organisations`.`id` = `organisation_members`.`organisation_id`
where `organisation_members`.`user_id` = 109 and `organisations`.`deleted_at` is null)'

但是,如果我修改用户模型,并删除 pivot 部分:

Class User {
   // ...

   public function organisations(): BelongsToMany
   {
        return $this->belongsToMany(Organisation::class, 'organisation_members');
   }
}

它 returns 正如预期的那样是一个空集合。

我该如何解决这个问题?

你的代码似乎在这里工作,即使在用户中有 pivot class

>>> return User::find(1)->organisations;
=> Illuminate\Database\Eloquent\Collection {#3464
     all: [
       App\Models\Organization {#3457
         id: 1,
         name: "hello",
         created_at: "2021-10-05 22:50:19",
         updated_at: "2021-10-05 22:50:19",
         pivot: Illuminate\Database\Eloquent\Relations\Pivot {#4251
           user_id: 1,
           organization_id: 1,
           role: "f",
         },
       },
     ],
   }
>>> return User::find(3)->organisations;
=> Illuminate\Database\Eloquent\Collection {#4396
     all: [],
   }
>>>

并且执行了相同的查询

select `organizations`.*, `organisation_members`.`user_id` as `pivot_user_id`, `organisation_members`.`organization_id` as `pivot_organization_id`, `organisation_members`.`role` as `pivot_role` from `organizations` inner join `organisation_members` on `organizations`.`id` = `organisation_members`.`organization_id` where `organisation_members`.`user_id` = 3

但是组织中的关系有误 Class :

class Organisation {
    public function organisationMembers(): HasMany
    {
        return $this->hasMany(OrganisationMember::class);
    }

    public function users(): BelongsToMany
    {
        return $this->belongsToMany(Organisation::class, 'organisation_members')
            ->using(OrganisationMember::class);
    }
}

您需要将用户 class 的 belongsToMany 第一个参数更改为这样

class Organisation {

    public function organisationMembers(): HasMany
    {
        return $this->hasMany(OrganisationMember::class);
    }

    public function users(): BelongsToMany
    {
        return $this->belongsToMany(User::class, 'organisation_members')
            ->using(OrganisationMember::class);
    }
}

在此处的文档中查看它:Laravel Eloquent