Reputation: 2310
I have the following form:
{{ Form::open(array('route' => 'get.index', 'method' => 'get')) }}
{{ Form::label('order', 'Order by') }}
{{ Form::select('order' , array('firstname' => 'First Name', 'lastname' => 'Last Name', 'state' => 'State')) }}
{{ Form::submit('Order results') }}
{{ Form::close() }}
I'd like to make the selected option be the one which corresponds to the order
variable (if any) in the query string. It should also default to the first choice if there isn't any query parameter.
Is this possible?
Upvotes: 0
Views: 85
Reputation: 5108
How about this:
<?php
$availableOrders = ['firstname' => 'First Name', 'lastname' => 'Last Name', 'state' => 'State'];
$selectedOrder = Input::get('order', null);
$selectedOption = !is_null($selectedOrder) && array_key_exists($selectedOrder, $availableOrders) ? $selectedOrder : 'firstname';
?>
{{ Form::select('order', $availableOrders, $selectedOption); }}
It's pretty selfdescriptive:
First you define an array of available order options.
Then you get the one that has to be selected from the query string.
If it's not null and an option with such array key exists - let's select it. If not - let's select the first one.
And once you have all these values you simply output it using blade and Form::select.
Don't forget that this can be done in fewer lines of code but this way the whole idea is very clear.
Upvotes: 1