I have a model called Condition
which has a column called expires_at
which stores a datetime.
Basically I need to get all Conditions that are about to expire. A Condition is about to expire when there are only 5 days left to reach the stored date.
How can I achieve this with Eloquent? I need to place the logic where the where
is.
Condition::where(xxx)->get()
PS. I am using Carbon in case this helps.
This code will return objects where expires_at
is between now and +5 days from now moment.
Condition::whereBetween('expires_at', [Carbon::now(), Carbon::now()->addDays(5)])->get()
you should use scope;
you should add method in Condition class .
public function scoperExpired($query) { $date = date('Y-m-d', strtotime('+5 day')); return $query->where('expires_at', '<=', $date);}
and now you can use expired as scope:-
Condition::expired()->get()
will return all expired conditions.