protonemedia / laravel-form-components

A set of Blade components to rapidly build forms with Tailwind CSS (v1.0 and v2.0) and Bootstrap 4/5. Supports validation, model binding, default values, translations, Laravel Livewire, includes default vendor styling and fully customizable!

Home Page:https://protone.media/blog/laravel-form-components-to-rapidly-build-forms-with-tailwind-css-and-bootstrap-4

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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)