verona

Make custom query builder method (query scope) for all models in Laravel 5.5

Posted 1 year ago by verona

I have multiple models, all with timestamps. I'm often using whereDate to get all rows from today and yesterday, like this:

ModelName::whereDate('created_at', now()->today())->get();
ModelName::whereDate('created_at', now()->yesterday())->get();

I want to have it shorter, simplier, like:

ModelName::today()->get();
ModelName::yesterday()->get();

I can not find any methods to do this, so I found in documentation that I can make own "scopes". The problem is, that I can make it for specified model, but I can not find a way to make it globally, for all models. Now I need to paste this scope methods in every model class. This works, but I need to repeat this code in every model class, so it's not a good way to do this I'm sure.

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class ModelName extends Model
{


    /**
     * Custom scope (query builder method) to easy return all items from today
     *
     * @param \Illuminate\Database\Eloquent\Builder $query
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function scopeToday($query)
    {
        return $query->whereDate('created_at', now()->today());
    }


    /**
     * Custom scope (query builder method) to easy return all items from yesterday
     *
     * @param \Illuminate\Database\Eloquent\Builder $query
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function scopeYesterday($query)
    {
        return $query->whereDate('created_at', now()->yesterday());
    }
}

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.