Today I want to share a small trick which I’ve found out only recently. Let’s say you need to have a select dropdown field with number range from X to Y – for example, birth year from 1900 to 2015. How would you do it?
In general PHP, you can write HTML and for loop:
<select name="year">
<? for ($year=1900; $year <= 2015; $year++): ?>
<option value="<?=$year;?>"><?=$year;?></option>
<? endfor; ?>
</select>
Of course, in Laravel you have Form::select and you can pass array to it:
Controller:
$years = []; for ($year=1900; $year <= 2015; $year++) $years[$year] = $year;
View:
{!! Form::select('year', $years) !!}
But seems also quite long for such a simple operation, right? Luckily, there is a better and more elegant way – meet function selectRange().
{!! Form::selectRange('year', 1900, 2015) !!}
And that’s it, here’s the dropdown result:

As you probably understood, selectRange() has three parameters – field name, range start number and end number. Unfortunately, there’s no parameter for step – so if you want to build a dropdown with numbers repeating every 2, every 5 or something – this function won’t work for you, you have to build it manually.
Bonus: selectYear() and selectMonth()
Also there are two extra similar functions in illuminate/html package.
Actually, there’s even shorter way to have a dropdown for years – with special function selectYear().
{!! Form::selectYear('year', 1900, 2015) !!}
It will produce the same result as the function selectRange() above.
And final tip – if you want to have dropdown of months – simple: selectMonth().
{!! Form::selectMonth('month') !!}
Here’s how the result looks:

So there you go – a few sweet helpers to make your code prettier and shorter.
No need to loop to create a range, PHP has a range function:
$years = range(1900, 2015);
Well, yes and no. range() works if you want index to be 0, 1, 2 etc.
So range(1900, 2015) would return [0 => 1900, 1 => 1901, …] – not [1900 => 1900, 1901 => 1901, …]
it nice to see this, cos i work on API most of the time and instead of view i use JSON… but, Just a suggestion …
You need to explicitly say to use some extra package https://laravelcollective.com/ cos this is true only in version 4.x but in 5.x version of Laravel it was removed from the core framework.
Thanks for the notice, Igor, you’re right. I did mention it in other articles on the topic, but not in this one. Will keep in mind.
Hi Povilas,
How can I add a default value to selectYear()
How can i add a initial text with
` $currentYear=date(“Y”);
selectYear(‘dateOfBirth’, 1900, $currentYear, ”, array(‘class’ => ‘form-control ‘,’id’ => ‘dateOfBirth’))`
Currently I can only get the values from 1900 to 2017
I need to add a text **Select Year** in the select field. How to make it with selectYear()
Here is how I did it, this will allow me to select the date of birth for a person age from16 years old 80 years.
echo Form::selectRange(‘year’, \Carbon\Carbon::now()->year – 80, \Carbon\Carbon::now()->year – 16, null, [‘class’ => ‘form-control’,’required’ => ”]);