Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable global scope on model show?

Tags:

laravel

I have a model called project which can be archived. Archived projects do not show on the index page and I do that by using global scope on the Project model:

class ArchiveScope implements Scope
{
    public function apply(Builder $builder, Model $model)
    {
        $builder->where('archived', false);
    }
}

then within the Project model, I have:

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

        static::addGlobalScope(new ArchiveScope);
    }

However, I have a page where I'm showing all archived projects and users should be able to click on each project, view the content, and un-archive the project if needed. But due to the global scope, it will not display the project. When I go to an archived project at /projects/{project_id}, it gives me 404 not found error. How can I apply withoutGlobalScope to the show operation of Project?

Thank you.

like image 929
Basharmal Avatar asked Jan 30 '26 06:01

Basharmal


1 Answers

You can do it

Project::withoutGlobalScope(ArchiveScope::class)->get();
like image 85
Thai Nguyen Hung Avatar answered Feb 01 '26 21:02

Thai Nguyen Hung



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!