Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Troj's avatar
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?

0 likes
12 replies
Troj's avatar
Level 4

@michaloravec Thanks man! The dropdown is exactly what i want, now i have to find out how to filter the results based on that dropdown. I will look into the docs.

Troj's avatar
Level 4

@michaloravec That works like a charm.

My complete controller now looks like this:

class SearchController
{

    public function index(Request $request)
    {

    	$activities = Activity::query();
    	$date = Carbon::parse($request->period);

    	if($request->get('category')) {
    		$activities->where('category_id', '=', $request->get('category'));
    	}
    			
    	if($request->get('city')) {
    		$activities->where('city_id', '=', $request->get('city'));
    	}

    	if($request->get('period')) {
    		$activities->whereMonth('starts_at', $date->month)
    			->whereYear('starts_at', $date->year);
    	}

    	$activities = $activities->get();

        return view('search.index', compact('activities'));
    }

}

@michaloravec do you happen know things about livewire as well, because now i have this (the above code). But would love to make the dropdown depend on eachother. Because now you can filter but run into trouble when there are no results for it.

Here's a my question where i try to apply this filtering with livewire dependend dropdown

Troj's avatar
Level 4

@michaloravec I used your code to make this dropdown with months and year. But for some reason february is missing.

	<select name="period" class="pl-10 text-base sm:text-sm mt-1 form-select block w-full text-gray-500 focus:bg-gray-100">
		<option value="">{{ __('When are you going?') }}</option>
	    @foreach(\Carbon\CarbonPeriod::create(now(), '1 month', now()->addMonths(11)) as $date)
	        <option value="{{ $date->format('F Y') }}" {{ $date->format('F Y') == request()->query('period') ? 'selected' : '' }}>
	            {{ $date->format('F Y') }}
	        </option>
	    @endforeach
	</select>
MichalOravec's avatar
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>
Troj's avatar
Level 4

@michaloravec Ah ok, i probably wouldn't have noticed if it wasn't the 31st today. Thanks again.

Troj's avatar
Level 4

@michaloravec I have one more question then I'll stop bothering you ;-)

I'm starting to see my query is not sufficient. The start and end dates can be null as well (ongoing activities)

  • Activities are also still available, in the requested month, when:
    • starts_at <= requested date && ends_at >= requested month && year = requested year
    • or when both starts_at = null && ends_at = null && year = null
    • or when starts_at = null && ends_at >= requested month && year = requested year
    • or when starts_at <= requested month && ends_at = null && year = requested year

So i changed it to this, but it's not doing as i expected. For example when both end and start date are null nothing is returned

    	if($request->get('period')) {
			$activities->whereMonth('starts_at', '<=', $date->month)
				->whereMonth('ends_at', '>=', $date->month)
				->whereYear('starts_at', $date->year)
				->whereYear('ends_at', $date->year)
	            ->orWhere(function($query) use ($date) {
	                $query->whereMonth('starts_at', '=', null)
	                	->whereMonth('ends_at', '=', null)
	                	->whereYear('starts_at', '=', null)
	                	->whereYear('ends_at', '=', null);
	            })
	            ->orWhere(function($query) use ($date) {
	                $query->whereMonth('starts_at', '=', null)
	                	->whereMonth('ends_at', '>=', $date->month)
	                	->whereYear('ends_at', '=', $date->year);
	            })
	            ->orWhere(function($query) use ($date) {
	                $query->whereMonth('starts_at', '<=', $date->month)
	                	->whereMonth('ends_at', '=', null)
	                	->whereYear('starts_at', '=', $date->year);
	            });
    	}

Can you give me a hint?

Troj's avatar
Level 4

@michaloravec I will try to figure this out, and otherwise i'll come back to you in another thread

Please or to participate in this conversation.