select multiple does not handle selected attribute
vwasteels opened this issue · comments
Hello,
When I do this :
@bind($model)
<x-form-select
multiple
name="categories[]"
:options="App\Models\Categories::options()"
/>
@endbind($model)
Categories are saved, but they are not selected on page reload : no selected
attribute on the <option>
tags ,
am I doing it wrong ?
BTW this is my App\Models\Categories::options()
:
[
1 => "Justice",
2 => "Culture",
3 => "Agriculture"
]
I am still stuck on this, if anyone knows how to set the "selected" attribute , that would be great :)
What is $model->categories
? Is it a relationship (what kind?) or an array?
yes it is a morphToMany
relation ,
I figured how to do it , by bypassing the model binding, not the nicest way, but it works :
<x-form-select
multiple
name="categories[]"
>
@foreach(App\Models\Categories::options() as $id => $option)
<option
value="{{ $id }}"
@if($model->categories->contains($id))
selected
@endif
>{{ $option }}</option>
@endforeach
</x-form-select>
Maybe we can add a relation
attribute to handle this use-case.
@bind($model)
<x-form-select multiple relation
name="categories[]"
:options="App\Models\Categories::options()" />
@endbind($model)
This has been added in 2.5.0:
https://github.com/protonemedia/laravel-form-components#using-eloquent-relationships