trifek
1 month ago
134
3
Laravel

Change form option to Laravel Collective

Posted 1 month ago by trifek

Hi, I use in my project Laravel 7 and https://laravelcollective.com/docs/6.0/html#drop-down-lists

I have this code:

<select name="category_id" class="form-control w-auto" required="required">
                                    <option value=""></option>
                                    @foreach($categories as $category)
                                        <option value="{{ $category['id'] }}"
                                                @if(isset($product) && $product->category_id
                                                == $category['id']) selected="selected"
                                            @endif>{{ $category['name'] }}
                                        </option>
                                    @endforeach
                                </select>

I want change it to Laravel collective.

in $categories I have:

array:19 [▼
  0 => array:2 [▼
    "id" => 1
    "name" => "- Books"
  ]
  1 => array:2 [▼
    "id" => 2
    "name" => "-- Comic Book"
  ]
  2 => array:2 [▶]
  3 => array:2 [▶]
  4 => array:2 [▶]
  5 => array:2 [▶]
  6 => array:2 [▶]
  7 => array:2 [▶]
  8 => array:2 [▶]
  9 => array:2 [▶]
  10 => array:2 [▶]
  11 => array:2 [▶]
  12 => array:2 [▶]
  13 => array:2 [▶]
  14 => array:2 [▶]
  15 => array:2 [▶]
  16 => array:2 [▶]
  17 => array:2 [▶]
  18 => array:2 [▶]
]

(it's work fine).

Now in my blade i'm try make my select/option:

<div class="form-group row">
                            {{Form::label('category_id', 'Kategoria produktu', ['class'=>'col-form-label text-left col-lg-3 col-sm-12'])}}
                            <div class="col-lg-9 col-sm-12">
                                {{ Form::select('category_id', $categories, data_get($product ?? 0, 'category_id') == '0', ['placeholder' => 'Wybierz kategorię', 'required' => 'required', 'class'=>'form-control w-autos']) }}
                            </div>
                        </div>

In result I have this: https://ibb.co/0Qj4v7k (with collective) not correct select, and correct: https://ibb.co/HC2VCxz (without collective)

How can I repair it?

Please sign in or create an account to participate in this conversation.