无法使用不同类型的相同模型定义 BelongsToMany 关系

Not able to define BelongsToMany relationship using same models with different type

我有模型组织和用户。组织可以有买家客户经理和卖家客户经理。

在组织中,关系如下:

public function managers() {
return $this->belongsToMany(User::class, 'organization_account_managers')->using('App\Model\Organization\OrganizationAccountManager')->withPivot(['account_manager_type']);    

}

在用户中,关系定义为:

public function accounts() 
{
    return $this->belongsToMany(Organization::class, 'organization_account_managers')
    ->using('App\Model\Organization\OrganizationAccountManager')
    ->withPivot(['account_manager_type']);
}

在 Nova 中附加时,已将组织定义为:

BelongsToMany::make('Account Managers','managers', 'App\Nova\User')
->fields(function () {
    return [
        Select::make('Type','account_manager_type')
                ->options(AppOrganization::$account_manager_types)
                ->rules('required')
                ->displayUsingLabels()->sortable(),
   ];
})

table结构是:

Schema::create('organization_account_managers', function (Blueprint $table) {
        $table->id();
        $table->foreignId('organization_id');
        $table->foreignId('user_id');
        $table->tinyInteger('account_manager_type');
        $table->timestamps();
});

问题陈述: 一个用户既可以是买家客户经理,也可以是卖家客户经理。但是当我尝试连接时,NOva 给我一个错误:这个用户已经连接了。

感谢任何有关如何解决此问题的想法。

来自版本 v3.23.0 Laravel Nova allows duplicate relations

BelongsToMany::make('Account Managers', 'managers', 'App\Nova\User')
    ->fields(function () {
        ...
    })
    ->allowDuplicateRelations()

如果您使用的是以前的版本,您可以尝试 this 解决方法。