Laravel $q->where() between dates

前端 未结 4 1064
死守一世寂寞
死守一世寂寞 2020-11-30 02:49

I am trying to get my cron to only get Projects that are due to recur/renew in the next 7 days to send out reminder emails. I\'ve just found out my logic doesn\

4条回答
  •  心在旅途
    2020-11-30 03:33

    You can chain your wheres directly, without function(q). There's also a nice date handling package in laravel, called Carbon. So you could do something 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 you require Carbon in composer and you're using Carbon namespace (use Carbon\Carbon;) and it should work.

    EDIT: As Joel said, you could do:

    $projects = Project::whereBetween('recur_at', array(Carbon::now(), Carbon::now()->addWeek()))
        ->where('status', '<', 5)
        ->where('recur_cancelled', '=', 0)
        ->get();
    

提交回复
热议问题