Support the ongoing development of →
Database Eloquent
Last updated 1 year ago.

Have you look at query scope?

Video also able

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. 



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

Some presenter packages,

Hope that helps


Sign in to participate in this thread!


Your banner here too?

ChrisT christ Joined 30 Jul 2015


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

Your logo here?

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

© 2023 - All rights reserved.