Multiple relationship fields #423
-
I need to implement some product - attributes logic into my project. I have 2 models - The problem is that i want to display multiple repeatable fields for adding attributes on Product CRUD. This is because attributes are organized by categories like Size, Color and i want to give the user a more easy way to read/follow what attributes. i have the following relationship on product model: public function atributesOptions()
{
return $this->belongsToMany(\App\Models\AttributeOption::class, 'attribute_option_product', 'product_id', 'attribute_option_id')->withPivot('extra_price', 'details')->withTimestamps();
}
public function sizeAttributes()
{
return $this->atributesOptions()->onlySizeCategory();
}
public function colorAttributes()
{
return $this->atributesOptions()->onlyColorCategory();
} and the following fields definition on product CRUD: [
'name' => 'sizeAttributes',
'label' => 'Size',
'type' => 'relationship',
'tab' => 'Atribute',
'subfields' => [
[
'name' => 'extra_price',
'type' => 'number',
'default' => 0,
'label' => 'Extra price',
'wrapper' => ['class' => 'form-group col-md-4'],
],
[
'name' => 'details',
'type' => 'text',
'label' => 'Details',
'wrapper' => ['class' => 'form-group col-md-4'],
],
],
'pivotSelect' => [
'label' => 'Size',
'placeholder' => 'choose size',
'wrapper' => ['class' => 'col-md-4'],
'options' => (function ($query) {
return $query->onlySizeCategory()->get();
}),
],
'new_item_label' => 'Add size',
'init_rows' => 1,
'min_rows' => 1,
'max_rows' => 4,
'reorder' => false,
],
[
'name' => 'colorAttributes',
'label' => 'Color',
'type' => 'relationship',
'tab' => 'Atribute',
'subfields' => [
[
'name' => 'extra_price',
'type' => 'number',
'default' => 0,
'label' => 'Extra price',
'wrapper' => ['class' => 'form-group col-md-4'],
],
[
'name' => 'details',
'type' => 'text',
'label' => 'Details',
'wrapper' => ['class' => 'form-group col-md-4'],
],
],
'pivotSelect' => [
'label' => 'Color',
'placeholder' => 'choose color',
'wrapper' => ['class' => 'col-md-4'],
'options' => (function ($query) {
return $query->onlyColorCategory()->get();
}),
],
'new_item_label' => 'Add color',
'init_rows' => 1,
'min_rows' => 1,
'max_rows' => 4,
'reorder' => false,
], The only problem i cannot solve is that on creating/saving action is saves only the last repeatable in the database (in the example above i get only colors saved in database). Any suggestion how to solve this? Thanks. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
Hmm interesting. The first thing that comes to mind is that you can use model events on your fields, to customize the saving process. That way, it's you who does the saving, instead of Backpack. So you could do: [
'name' => 'sizeAttributes',
'label' => 'Size',
'type' => 'relationship',
'tab' => 'Atribute',
'subfields' => [
// ...
],
'pivotSelect' => [
// ...
],
// ...
'events' => [
'saving' => function ($entry) {
// TODO: your saving logic here
},
],
], But this does make me wonder... WHY?! Why does it only save the last one? It's probably because behind the scenes Backpack will turn that "fake" relationship into the "real" relationship. But... then... isn't this like sort of a bug? What do you think @pxpm - is this worth supporting? |
Beta Was this translation helpful? Give feedback.
-
thanks guys! from @o15a3d4l11s2 answer i've managed to concatenate all the inputs and do one single sync in create/update operation. |
Beta Was this translation helpful? Give feedback.
The reason for this is https://github.com/Laravel-Backpack/CRUD/blob/f180f5e4bb900640f5c07abf5e1c1fbb70ccf6d7/src/app/Library/CrudPanel/Traits/Create.php#L145 which does a
sync
. When the last "fake" field that uses the relationship is stored, it overwrites previously stored relationships.I do not think this is a bug, although users might expect a different behaviour.
A "workaround" solution inside Backpack would be non-trivial, as it would need to pre-determine if such relationship-sharing fields exist. If we iterate over all relationship fields and "merge" them into relationship fields with the datas also merged, this would work.
But there are a lot of possible structures for the rela…