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

justgkp's avatar

how to filter data by month instead of date in laravel?

I am new to laravel and I could filter data by date but I don't know how to filter the same by month. Below is the controller function and views:

public function photo_gallery_show(Request $request) {

    $gallery_data = PhotoGallery::when($request->date != null, function ($q) use ($request) {

                    return $q->whereDate('event_date', $request->date);

                    })
                    ->paginate(10)
                    ->sortByDesc('event_date');
Select Month: Filter
    return view('front.photo_gallery', compact('gallery_data'));
}
0 likes
5 replies
Snapey's avatar
Snapey
Best Answer
Level 122

use whereMonth and whereYear()

1 like
justgkp's avatar

@Snapey Thanks a lot.... i fixed it using whereMonth nd whereYear!!!

justgkp's avatar

One small issue... Normal pagination working fine but pagination not working properly in filtering. While filtering, instead of going to next page, after clicking next, it's redirected to the main page. Any solution for this???

Please or to participate in this conversation.