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

myke2424's avatar

Query by month and year

I'm trying to query by the month and year but it's returning an empty array for each month. Here's my function

    public function month($id, Request $request){
        $user = auth()->user();
        $month = $request->month;
        $year = $request->year;

        dd($times = Time::where('user_id', $id)
        ->whereYear('start_day', $request->year)
        ->whereMonth('start_day', $request->month)
        ->get());
       

        return view ('admin.report.month', compact('month', 'year', 'user'));
    }

All my requests are working properly.

0 likes
2 replies
Nana-Odai's avatar

What is the request returning.? in which format of month? Note, the start_day format must be same as the year. so if you have start_day as 2019 in your db, then your $request->year must be of type year as 2019. same as the months.

myke2424's avatar

I got it working, the issue was my whereMonth. I had to parse the month and return the number for the year.

Please or to participate in this conversation.