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

AbdulBazith's avatar

Compare only month and year in db and fetch records

i have a form to choose date from blade file that i pass in the controller.

$request->salary_date is my date textbox name which is in date format.

and in my db also i have salary_date column which is also date format.

now i need to compare only the month and year from the $request->salary_date with salary_date in the db and get those records

this is my query


  $empsalaries = EmpSalary:: where('salary_date, ''????????")->get()

just need to compare only month and year in the db salary_date column

guys checked in google well cant get idea

0 likes
5 replies
Nakov's avatar
Nakov
Best Answer
Level 73

Don't know the format of your salary_date field, so let's assume that it is DD/MM/YYYY, you can create a Carbon instance out of it and then use Eloquent to find the match like this:

$date = Carbon::createFromFormat('d/m/Y', $request->salary_date);

$empsalaries = EmpSalary:: whereMonth('salary_date', $date->month)
    ->whereYear('salary_date', $date->year)
    ->get()

1 like
AddWebContribution's avatar

You should try like this:

$post = Mjblog::whereYear('created_at', '=', $year)
              ->whereMonth('created_at', '=', $month)
              ->get();
1 like
AbdulBazith's avatar

@nakov thank you for your response.

my salary_date format in db is 2019-08-28 year-month- date

i tried like this

 $date = Carbon::createFromFormat('Y-m-d', $request->salary_date);

but showing error

InvalidArgumentException
Data missing

whats the problem. in google some says add time also so i added H:m:s also but not working same error

Nakov's avatar

@abdulbazith I think that your $request->salary_date is empty that's the reason why you get that error. So please check before you try to format it if you really send the salary_date from an input field in your view.

Please or to participate in this conversation.