Support the ongoing development of Laravel.io →
Requests Input Eloquent

Hey All,

I have a Quote model with a table column boolean('approved') which I want to use to setup two global scopes for "approved" and "not approved" quotes:

ApprovedScope.php

class ApprovedScope implements Scope
{
    public function apply(Builder $builder, Model $model)
    {
        return $builder->where('approved', true);
    }
}

Quote model:

class Quote extends Model

    protected static function boot()
    {
        parent::boot();

        static::addGlobalScope(new ApprovedScope);
        // static::addGlobalScope(new NotApprovedScope);
    }

    public function author()
    {
    	return $this->belongsTo('App\Author');
    }

    public function category()
    {
    	return $this->belongsTo('App\Category');
    }

You of course can't boot the model into two global scopes. But is there a solution to use both global scopes and define which one to use at controller level? Or the only way here is using Local Scopes?

Appreciate any ideas.

Last updated 3 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

mindio mindio Joined 14 Jun 2016

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.

© 2025 Laravel.io - All rights reserved.