Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel $q->where() between dates

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't quite work.

I currently have the 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, I realized what I need to do is something like:

Psudo SQL:

SELECT * FROM projects WHERE recur_at > recur_at - '7 days' AND /* Other status + recurr_cancelled stuff) */ 

How would I do this in Laravel 4, and using the DATETIME datatype, I've only done this sort of thing using timestamps.

Update:

Managed to solve this after using the following code, Stackoverflow also helps when you can pull bits of code and look at them out of context.

$projects = Project::where(function($q){     $q->where(DB::raw('recur_at BETWEEN DATE_SUB(NOW(), INTERVAL 7 DAY) AND NOW()'));     $q->where('status', '<', 5);     $q->where('recur_cancelled', '=', 0); }); 

Updated Question: Is there better way to do this in Laravel/Eloquent?

Update 2:

The first resolution ended up not been right after further testing, I have now resolved and tested the 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); }); 
like image 840
Jono20201 Avatar asked Jul 18 '14 12:07

Jono20201


People also ask

How can we get data between two dates using query in laravel?

Get data between two dates with MySQL Raw Query We'll need to resort to DB:raw() to replicate this with Eloquent, which would look like this: $startDate = '2022-06-01'; $endDate = '2022-06-30'; Post::whereBetween(DB::raw('DATE(created_at)'), [$startDate, $endDate])->get();

How do I use whereBetween in laravel?

The whereBetween() method is a query builder chained alongside other Laravel query builders used to fetch data from the database. The whereBetween() method queries the database table to fetch rows of records from the database within a range of values.


1 Answers

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(); 
like image 108
Tom Avatar answered Oct 11 '22 06:10

Tom