php - Laravel $q->where() between dates -


i trying cron projects due recur/renew in next 7 days send out reminder emails. i've found out logic doesn't quite work.

i have query:

$projects = project::where(function($q){     $q->where('recur_at', '>', date("y-m-d h:i:s", time() - 604800));     $q->where('status', '<', 5);     $q->where('recur_cancelled', '=', 0); }); 

however, realized need like:

psudo sql:

select * projects recur_at > recur_at - '7 days' , /* other status + recurr_cancelled stuff) */ 

how in laravel 4, , using datetime datatype, i've done sort of thing using timestamps.

update:

managed solve after using following code, stackoverflow helps when can pull bits of code , @ them out of context.

$projects = project::where(function($q){     $q->where(db::raw('recur_at between date_sub(now(), interval 7 day) , now()'));     $q->where('status', '<', 5);     $q->where('recur_cancelled', '=', 0); }); 

updated question: there better way in laravel/eloquent?

update 2:

the first resolution ended not been right after further testing, have resolved , tested following solution:

$projects = project::where(function($q){     $q->where('recur_at', '<=', carbon::now()->addweek());     $q->where('recur_at', '!=', "0000-00-00 00:00:00");     $q->where('status', '<', 5);     $q->where('recur_cancelled', '=', 0); }); 

you can chain wheres directly, without function(q). there's nice date handling package in laravel, called carbon. like:

$projects = project::where('recur_at', '>', carbon::now())     ->where('recur_at', '<', carbon::now()->addweek())     ->where('status', '<', 5)     ->where('recur_cancelled', '=', 0)     ->get(); 

just make sure require carbon in composer , you're using carbon namespace (use carbon\carbon;) , should work.

edit: joel said, do:

$projects = project::wherebetween('recur_at', array(carbon::now(), carbon::now()->addweek()))     ->where('status', '<', 5)     ->where('recur_cancelled', '=', 0)     ->get(); 

Comments

Popular posts from this blog

javascript - Create a stacked percentage column -

Optimising Firebase database by automatically overwriting data -

javascript - Angular UI-Grid customTemplate directive causing rows to load slowly/? -