40

I need to give selected value like this html:

<select name="myselect" id="myselect">
 <option value="1">Item 1</option>
 <option value="2" selected='selected'>Item 2</option>

how can I achieve this, with laravel forms?

17 Answers 17

73

Everybody talking about you go using {!! Form::select() !!} but, if all you need is to use plain simple HTML.. here is another way to do it.

<select name="myselect">
@foreach ($options as $key => $value)
    <option value="{{ $key }}"
    @if ($key == old('myselect', $model->option))
        selected="selected"
    @endif
    >{{ $value }}</option>
@endforeach
</select>

the old() function is useful when you submit the form and the validation fails. So that, old() returns the previously selected value.

Sign up to request clarification or add additional context in comments.

Comments

27

You can do it like this.

<select class="form-control" name="resoureceName">

  <option>Select Item</option>

  @foreach ($items as $item)
    <option value="{{ $item->id }}" {{ ( $item->id == $existingRecordId) ? 'selected' : '' }}> {{ $item->name }} </option>
  @endforeach    </select>

Comments

15

You have to set the default option by passing a third argument.

{{ Form::select('myselect', [1, 2], 2, ['id' => 'myselect']) }}

You can read the documentation here.

2 Comments

that's for Laravel 4.2 .. If you want to use Form::select on Laravel 5.1+ you need to pull in laravelcollective/html package
what if it was a multi select where multiple items are checked? i have a multi-select with checkboxes, see multiselect-ui
13

use this package and check the docs:

https://laravelcollective.com/docs/5.2/html#drop-down-lists

form you html , you need use this mark

{!! Form::select('size', array('L' => 'Large', 'S' => 'Small'), 'S'); !!}

3 Comments

Laravel Collective has been discontinued :(
The HTML package is still maintained though… github.com/LaravelCollective/html/issues/600
The HTML package and the Remote package are still supported.
11

Another ordinary simple way this is good if there are few options in select box

<select name="job_status">
   <option {{old('job_status',$profile->job_status)=="unemployed"? 'selected':''}}  value="unemployed">Unemployed</option>
   <option {{old('job_status',$profile->job_status)=="employed"? 'selected':''}} value="employed">Employed</option>
</select>

Comments

6

Laravel 9:

@php
    $item=2;
@endphp

<select name="myselect" id="myselect">
 <option value="1">Item 1</option>
 <option value="2" @selected($item == 2) >Item 2</option>
</select>

Laravel docs

Comments

5

Setting selected option is very simple in laravel form :

{{ Form::select('number', [0, 1, 2], 2) }}

Output will be :

<select name="number">
  <option value="0">0</option>
  <option value="1">1</option>
  <option value="2" selected="selected">2</option>
</select>

1 Comment

you answered same answer as @chonchol about 2 hrs later? i would poin tit, but i pointed first to answer. or did somebody edit theirs?
5
            @foreach ($categories as $category)
             <option value="{{$category->id}}" 
               @foreach ($posts->postRelateToCategory as $Postcategory)
                 @if ($Postcategory->id == $category->id)
                 {{'selected="selected"'}}
                 @endif 
               @endforeach >
              {{ $category->category_name }} </option>               
            @endforeach    

Comments

5
  <?php
      $items = DB::table('course')->get()->pluck('name','id');
      $selectID = 3;
  ?>

  <div class="form-group">
   {{ Form::label('course_title', 'Course Title') }}
   {!! Form::select('myselect', $items, $select, ['class' => 'form-control']) !!}
  </div>

This show similar types of following options :

<select name="myselect" id="myselect">
 <option value="1">Computer Introduction</option>
 <option value="2">Machine Learning</option>
 <option value="3" selected='selected'>Python Programming</option>
 <option value="4">Networking Fundamentals</option>
 .
 .
 .
 .  
</select>

Comments

5

Try this

<select class="form-control" name="country_code" value="{{ old('country_code') }}">
   @foreach (\App\SystemCountry::orderBy('country')->get() as $country)
       <option value="{{ $country->country_code }}"
           @if ($country->country_code == "LKA")
             {{'selected="selected"'}}
           @endif
       >
          {{ $country->country }}
       </option>
   @endforeach
</select>

1 Comment

It looks like your post is mostly code, please add some more details
3

To echo some other answers here, the code I just used with 5.6 is this

{{ Form::select('status', ['Draft' => 'Draft', 'Sent' => 'Sent', 'Paid' => 'Paid'], $model->status, ['id' => 'status']) }}

In order to be able to use the Form Helper from LaravelCollective I took a look at https://laravelcollective.com/docs/master/html#drop-down-lists

I also had to composer require the dependency also so that I could use it in my projects

composer require "laravelcollective/html":"^5"

Lastly I altered my config/app.php and added the following in the $aliases array

    'Form' => Collective\Html\FormFacade::class,

https://laravelcollective.com/docs/master/html should be consulted if any of the above ceases to work.

Comments

2

If you have an Eloquent Relationship between your models you can do something like that:

@foreach ($ships as  $ship)
<select name="data[]" class="form-control" multiple>
    @foreach ($goods_containers as  $container)
        <option value="{{ $container->id }}"
                @if ($ship->containers->contains('container_id',$container->id ) ))
                selected="selected"
                @endif
        >{{ $container->number}}</option>
    @endforeach
</select>
@endforeach

Comments

1

Just Simply paste this code you will get the desired output that you needed.

 {{ Form::select ('myselect', ['1' => 'Item 1', '2' => 'Item 2'], 2 , ['id' =>'myselect']) }}` `

2 Comments

Just Simply paste this code you will get the desired output that you needed.
Thanks Ashish making the code on proper indentation i am new to stackoverflow. any ways i think this code will solve your problem
1

You can also try this for limited options:

          <select class="form-control required" id="assignedRole">
            <option id = "employeeRole" selected ="@if($employee->employee_role=='Employee'){'selected'}else{''} @endif">Employee</option>
            <option id = "adminRole" selected ="@if($employee->employee_role=='Admin'){'selected'}else{''} @endif">Admin</option>
            <option id = "employerRole" selected ="@if($employee->employee_role=='Employer'){'selected'}else{''} @endif">Employer</option>
          </select>

Comments

0

If anyone is still here, here is my simple version.

<select name="status" class="js-example-basic-single w-100">
              <option value="" @if($brand->status == '') ? selected : null @endif disabled>Choose what to be seen on brand section</option>
              <option value="TA" @if($brand->status == 'TA') ? selected : null @endif>Title Active</option>
              <option value="ITA" @if($brand->status == 'ITA') ? selected : null @endif>Image Title Active</option>
             
            </select>

Comments

0

This seems to be the shortest code

<select class="form-control" name="country_code">
    @foreach (\App\SystemCountry::orderBy('country')->get() as $country)
       <option value="{{ $country->country_code }}"
           @if ($country->country_code == "LKA") selected="selected" @endif >{{ $country->country }}
       </option>
    @endforeach
</select>

Comments

0

If you don't have laravel form use simpe {{ }}

<option value="days" {{ $generaltask->frequency === "days" ? "selected" : "" }}>Days</option>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.