0

I´m trying to create a form select in laravel.

I was created perfectly, but in my view first option is 0 and one first option empty enter image description here

I need to remove this 0 and option empty, but i don't know how i can do it, this is my actual code:

<div class="form-group row ">
        {!! Form::label('restaurant_id', trans("lang.blog_restaurant_id"),['class' => 'col-3 control-label text-right']) !!}
        <div class="col-9">
            {!! Form::select('restaurant_id', [null => null, $restaurant], null, ['class' => 'select2 form-control']) !!}
            <div class="form-text text-muted">{{ trans("lang.blog_restaurant_id_help") }}</div>
        </div>
    </div>

$restaurant it's one data array for to fill my select. I read that with first option null put selected option empty, but generate a empty option and 0.

Thanks for help me.

UPDATE

Laravel include optgroup into Form::select that it´s triggering my 0 this it´s the problem. i need delete optgroup

  <select class="select2 form-control select2-hidden-accessible" id="restaurant_id" name="restaurant_id" tabindex="-1" aria-hidden="true">
<option value="" selected="selected"></option>

    <optgroup label="0">
    
        <option value="17">Foody Lindgren, Cremin and Erdman gdfgfg fgfg fg fgfg g fggf</option>
        <option value="28">restaurante de pruebas</option>
        <option value="29">probando</option>
        <option value="30">pincho de castilla2</option>
    
    </optgroup>

update 2

getting list of restaurant:

$restaurant = $this->restaurantRepository->pluck('name', 'id');

this is result in blade:

{"17":"Foody Lindgren, Cremin and Erdman gdfgfg fgfg fg fgfg g fggf","28":"restaurante de pruebas","29":"probando","30":"pincho de castilla2"}

Add toArray():

$restaurant = $this->restaurantRepository->pluck('name', 'id');
$restaurant->toArray();

same result

daviserraalonso
  • 75
  • 1
  • 13

2 Answers2

0

Please try this. Add blank key and blank value like below.

{!! Form::select('restaurant_id', ["" => "", $restaurant], null, ['class' => 'select2 form-control']) !!}
0

It seems that the $restaurant is an array. So, you'd better do something like this:

Form::select('restaurant_id', $restaurant, null, ['class' => 'select2 form-control'])

you may need a proper mapping for your $restaurant in your controller, depending on what you need, like adding an empty option in the beginning.

// in Controller
$options = ["" => "select"] + $restaurant;

// In view
Form::select('restaurant_id', $options, null, ['class' => 'select2 form-control'])

update 2

You should make sure to send the $restaurant as an array:

$restaurant = $this->restaurantRepository->pluck('name', 'id')->toArray();

For adding an empty option:

$restaurant = ["" => "select"] + $restaurant;

There is also another option: You can even add the select box manually instead of using Form::select.

<select class="select2 form-control" id="restaurant_id" name="restaurant_id">
    <option value="" selected="selected"></option>
    @foreach($restaurant as $value => $option)
    <option value="{{ $value }}">{{ $option }}</option>
    @endforeach
</select>
Ahmad
  • 106
  • 1
  • 5
  • thanks for your response. i added your code in my controller and my blade, and returned: Object of class Illuminate\Support\Collection could not be converted to number – daviserraalonso Feb 17 '21 at 13:03
  • The `$restaurant` or `$options` should be an array. So, if it is a collection, you can make sure you provide an array by adding `$restaurant->toArray()`. – Ahmad Feb 17 '21 at 13:30
  • thaks for your help, i was updated my question with update 2 and add my function controller where i get my restaurant and my result. i was trayed to add toArray() i´m getting same result – daviserraalonso Feb 17 '21 at 17:10
  • try `$restaurant = $this->restaurantRepository->pluck('name', 'id')->toArray();` – Ahmad Feb 17 '21 at 17:52
  • it´s ok your code. but i´m traying with things and i can show that it´s a multidimensional array. if i do ["0" => $restaurant][0] i have my data without label '0' but also i need option empty and here i don´t know how i can to do – daviserraalonso Feb 17 '21 at 17:58