通过BelongsToMany附加关系时出现array_key_exists()错误

时间:2020-05-03 15:40:00

标签: php laravel eloquent laravel-nova laravel-7

  • Laravel版本:7.9.2
  • Nova版本:3.5.0
  • PHP版本:7.4.4

说明:

设置我的BelongsToMany关系后,每当我在任何资源上使用BelongsToMany Nova字段并且尝试附加关系时,都会出现以下一般错误:

image

array_key_exists(): The first argument should be either a string or an integer {"userId":1,"exception":"[object] (ErrorException(code: 0): array_key_exists(): The first argument should be either a string or an integer at

**************/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php:356)

如果我通过数据库中的数据透视表手动附加关系,则该关系在视图字段中可见,如下所示:

image

复制步骤:

class ProductCategory extends Pivot
{
    protected $table = 'product_category';
    protected $primaryKey = ['product_id','category_id'];
    public $increments = true;
}
class Category extends Model
{
    protected $table = 'categories';
    public $incrementing = true;
    protected $guarded = [];

    public function products()
    {
        return $this->belongsToMany(Product::class)->using(ProductCategory::class);
    }
}
class Product extends Model
{
    public function categories()
    {
        return $this->belongsToMany(Category::class, 'product_category')->using(ProductCategory::class);
    }
}
class Product extends Resource
{
    public static $model = 'App\Product';

    public static $title = 'id';

    public function fields(Request $request)
    {
        return [
            ID::make()->sortable(),
            BelongsToMany::make('Categories'),
        ]
     }
}
class Category extends Resource
{
    public static $model = 'App\Category';

    public function title()
    {
        return $this->id . " : " . $this->name;
    }

    public function fields(Request $request)
    {
        return [
            ID::make()->sortable(),
            Text::make('Name')->rules('required'),
            BelongsTo::make('Parent','parent', Category::class)->nullable(),
            Text::make('Code')->rules('required'),
            Text::make('Slug')->rules('required'),
            Textarea::make('Description')->rows(3)
        ];
    }
}

0 个答案:

没有答案