Ошибка array_key_exists () при подключении отношений через BelongsToMany - PullRequest
0 голосов
/ 03 мая 2020
  • Laravel Версия: 7.9.2
  • Новая версия: 3.5.0
  • PHP Версия: 7.4.4

Описание :

После настройки моих отношений BelongsToMany, всякий раз, когда я использую поле BelongsToMany Nova на любом из моих ресурсов, и я пытаюсь прикрепить связь, я получаю следующее обобщенное сообщение c error:

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)
        ];
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...