Laravel SQL selects a query where DateTimestamp contains this month and year

I am trying to make an SQL query where I can check if DateTimestamp contains this month and year.

Part of the request:

$leads = DB::table('leads') ->leftJoin('invoices', function($join2) { $join2->on('leads.id', '=', 'invoices.lead_id'); }) ->where('invoices.chance', '!=', 0) ->where('invoices.updated_at', '=', date('m-Y')) //Check if DateTimestamp(updated_at) contains Year and month ->select('leads.*') 

I tried this with date_parse, the new DateTime (), but that didn't work. What am I doing wrong?

+5
source share
1 answer

You can use whereMonth() and whereYear() to achieve this:

 $leads = DB::table('leads') ->leftJoin('invoices', function($join2) { $join2->on('leads.id', '=', 'invoices.lead_id'); }) ->where('invoices.chance', '!=', 0) ->whereMonth('invoices.updated_at', '=', date('m')) // changed ->whereYear('invoices.updated_at', '=', date('Y')) // changed ->select('leads.*'); 
+3
source

Source: https://habr.com/ru/post/1237855/


All Articles