Support the ongoing development of Laravel.io →
Database Eloquent
Last updated 2 years ago.
0

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(); 

0

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?

0

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

0

Sign in to participate in this thread!

Eventy

Your banner here too?

ChrisT christ Joined 30 Jul 2015

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.