Reputation: 742
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?
Upvotes: 40
Views: 230157
Reputation: 60
If you don't have laravel form use simpe {{ }}
<option value="days" {{ $generaltask->frequency === "days" ? "selected" : "" }}>Days</option>
Upvotes: 0
Reputation: 61
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>
Upvotes: 6
Reputation: 29
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>
Upvotes: 0
Reputation: 1469
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>
Upvotes: 0
Reputation: 87
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>
Upvotes: 5
Reputation: 3723
<?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>
Upvotes: 5
Reputation: 293
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>
Upvotes: 1
Reputation: 1769
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
Upvotes: 2
Reputation: 1078
@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
Upvotes: 5
Reputation: 1623
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.
Upvotes: 3
Reputation: 1314
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>
Upvotes: 27
Reputation: 2735
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.
Upvotes: 15
Reputation: 10113
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>
Upvotes: 11
Reputation: 2505
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'); !!}
Upvotes: 13
Reputation: 1230
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.
Upvotes: 73
Reputation: 9135
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']) }}` `
Upvotes: 1
Reputation: 2156
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>
Upvotes: 5