Reputation: 517
I have an Eloquent model, Day, that contains an activate_date
field which holds a Carbon object. I would like to return all of the days whose activate_date
is within, for example, 7 days of today. I was trying to do it with a scope, but I can't seem to access the activate_date
field within the scope function. Is there an easy way to do this?
Upvotes: 2
Views: 56
Reputation: 10390
You can do this easily:
$dateRef = new \Carbon::Carbon()
$dateRef->addDays(7);
// or $dateRef->subDays(7);
Day::where('activate_date', '<', $dateRef)->get();
with query scope:
public function scopeLast7Days($query)
{
$dateRef = new \Carbon::Carbon()
$dateRef->subDays(7);
return $query->whereBetween('activate_date', [$dateRef, new \Carbon::Carbon()])
}
// use like this:
$days = Day::last7Days()->active()->orderBy('created_at')->get();
Upvotes: 1