Hi October,
I am facing an issue while trying to save a belongsToMany relationship with pivot data.
All was going well, until I introduced a pivotModel
class Bundle extends Model
{
public $belongsToMany = [
'compositions' => [
Product::class,
'table' => 'prd_product_composition',
'pivotModel' => Composition::class,
'key' => 'product_id',
'otherKey' => 'composition_id',
'pivot' => ['min_quantity']
],
];
}
and this is the Pivot model
class Composition extends Pivot
{
public function product(): BelongsTo
{
return $this->belongsTo(Product::class, 'composition_id');
}
public function getTotalPriceAttribute(): float
{
return $this->min_quantity * $this->product->selling_price;
}
}
I used RelationController to manage the data,
The error appears when I try to save the relation data.
"Illuminate\Database\Eloquent\Model::save(): Argument #1 ($options) must be of type array, null given, called in /home/.../modules/backend/behaviors/relationcontroller/HasPivotMode.php on line 141" on line 1113 of /home/.../vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php
By the hint from error message, I change modules/backend/behaviors/relationcontroller/HasPivotMode.php line 141 & 119
from $modelToSave->save(null, $this->pivotWidget->getSessionKey());
to $modelToSave->save([], $this->pivotWidget->getSessionKey());
and everything is running again.
Is there something wrong with my code or any advice in this case?
Thank you