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?
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.
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.
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'); !!}
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>
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>
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>
<?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>
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>
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.
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
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']) }}` `
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>
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>
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>