2015-12-10 7 views
5

Sto provando a creare una query SQL in cui posso verificare se il DateTimestamp contiene questo mese e anno.Laravel SQL seleziona la query dove DataTimestamp contiene questo mese e anno

Una parte della query:

$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.*') 

ho provato con date_parse, nuova DateTime(), ma non ha funzionato. Che cosa sto facendo di sbagliato?

risposta

3

È possibile utilizzare whereMonth() e whereYear() per raggiungere questo obiettivo:

$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.*'); 
Problemi correlati