Closed shabaz-ejaz closed 2 weeks ago
@shabaz-ejaz I see the relation's name in your Piano model is called comparablePianos
but in the multi-select field it is in snake-case comparable_pianos
.
Twill uses eager loading when accessing model relationships so, $item>comparable_pianos
would return null
because it is not a property of the Piano model. Updating your multi-select field to
@formField('multi_select', [
'name' => 'comparablePianos',
'label' => 'Comparable Pianos',
'options' => $comparable_pianos
])
will work fine
@zeezo887 Thanks, I've tried that and now it doesn't actually save the the values in the pivot table.
Weirdly it did save when I have it like this:
@formField('multi_select', [
'name' => 'comparable_pianos',
'label' => 'Comparable Pianos',
'options' => $comparable_pianos2,
])
@shabaz-ejaz you should update your PianoRepository too
public function afterSave($object, $fields)
{
$object->comparablePianos()->sync($fields['comparablePianos'] ?? []);
parent::afterSave($object, $fields);
}
@zeezo887 That's what it was! That works great now thanks.
Description
When using Multi select with dynamic values, I have got the data saving fine, however when reloading the form, the selected values are not being checked in the form by default.
Steps to reproduce
I have a pivot table which joins a
pianos
table with itself to create 'comparable_pianos'.Pivot model:
PianoController:
PianoRepository:
Blade file:
Expected result
I expect the multiselect to have pre-selected values based on the ones in the database on form edit. However I only ever see the options with all of them unchecked.
Actual result
The values do save in the database, however are not shown as selected when reading from the table int he form.
Versions
Twill version: 2.0.1 Laravel version: 7.30.6 PHP version: 7.4.33 Database engine: MySQL InnoDB