<select>
@foreach(CarbonPeriod::create(now(), '1 month', now()->addMonths(11)) as $date)
<option value="{{ $date->format('F Y') }}">
{{ $date->format('F Y') }}
</option>
@endforeach
</select>
Dec 30, 2020
12
Level 4
Dropdown select with current month and the next 11 months to come
I want to filter my results with a starts_at date. I have a dropdown select in mind where the current month and year is the first option, and then the next 11 months as the next options.
something like this:
<-- Select a period -->
- December 2020
- January 2021
- February 2021
and so on.
Once that select dropdown is in place, i thought of filtering the results like so:
if($request->get('period')) {
$activities->whereMonth('starts_at', '=', $request->get('period'));
}
My question: how would I create that dropdown where it's always checking the current month we're in and then generates the dropdown with the current and the next 11 months?
Level 75
@troj Add there ->startOfMonth(), because today is 31st December and February doesn't have 31 days.
<select>
@foreach(CarbonPeriod::create(now()->startOfMonth(), '1 month', now()->addMonths(11)->startOfMonth()) as $date)
<option value="{{ $date->format('F Y') }}">
{{ $date->format('F Y') }}
</option>
@endforeach
</select>
Please or to participate in this conversation.