daviserraalonso
daviserraalonso

Reputation: 85

Fom::select in laravel empty for default

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

Upvotes: 0

Views: 1700

Answers (2)

Ahmad
Ahmad

Reputation: 106

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>

Upvotes: 0

Tansukh Rathod
Tansukh Rathod

Reputation: 90

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

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

Upvotes: 0

Related Questions