Reputation: 1439
I want to fetch tasks from my database that are starting at different times. i.e Starting Today, Tomorrow, Next Week and Next Month. Am trying to write scope in my model to handle that but I don't really know how to do it.
Here are my scope methods
/**
* @param $query
* @return mixed
*/
public function scopeToday($query)
{
return $query->where('start_at','=', Carbon::now());
}
/**
* @param $query
* @return mixed
*/
public function scopeTomorrow($query)
{
return $query->where('start_at','=', Carbon::now()->addDay(1));
}
/**
* @param $query
* @return mixed
*/
public function scopeNextWeek($query)
{
return $query->whereRaw('start_at = ?', [Carbon::now()->addWeek()]);
}
/**
* @param $query
* @return mixed
*/
public function scopeNextMonth($query)
{
return $query->where('start_at','=', Carbon::now()->addMonth(1));
}
Any one with a clue why the above ain't working?
Note: start_at is already a Carbon instance.
Upvotes: 0
Views: 7967
Reputation: 5958
I guess you start_at
field is datetime type
. And this might cause the problem on comparing date.
You need to convert datetime
to date
only and use Carbon::today()
instead of Carbon::now()
.
public function scopeToday($query)
{
return $query->where( DB::raw('DATE(created_at)') ,'=', Carbon::today());
}
This shows the different between Carbon::today()
and Carbon::now()
$now = Carbon::now();
echo $now; // 2015-03-26 00:36:47
$today = Carbon::today();
echo $today; // 2015-03-26 00:00:00
source: http://carbon.nesbot.com/docs/#api-instantiation
Upvotes: 1