Have you look at query scope?
http://laravel.com/docs/5.1/eloquent#query-scopes
Video also able https://laracasts.com/series/laravel-5-fundamentals/episodes/11
It's little bit different but here is how I use to query to get the post within in this month
public function scopeThismonth($query)
{
$now = Carbon::now();
$startOfThisMonth = Carbon::instance($now)->startOfMonth();
$currentOfThisMonth = Carbon::instance($now)->subSecond();
$query = $query->whereBetween('created_at',
[$startOfThisMonth, $currentOfThisMonth]);
return $query;
}
// This is how I get how many post is posted this month.
Post::thismonth()->count();
I'm also struggling to perform date calculations. The problem I have is that I'm trying to filter my query to only show records where the time difference between two dates within the database matches my criteria. For example, only show articles where updated_at is at least once month newer than created_at. I have tried whereRaw to use date_add functionality, but nothing seems to work within the scope?
I would keep the db at UTC, then use model presenters to convert it for user display.
Some background but for model presenters in L4, but still has some explanaitons http://culttt.com/2014/03/03/model-presenters-laravel-4/
Some presenter packages,
https://github.com/robclancy/presenter
https://github.com/laravel-auto-presenter/laravel-auto-presenter
Hope that helps
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community