Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

tanmay_das's avatar

Laravel global query scope's withoutGlobalScope() not returning desired records

I have a global query scope called ArchiveScope that mimics the similar functionality of Soft Deletion. The apply method of that scope looks like this:

    public function apply(Builder $builder, Model $model)
    {
        $builder->where('archived_at', '=', NULL);
    }

So when I use MyModel::all(), it returns all the rows that do not have a timestamp (i.e. NULL). But when I want to fetch all the records (including archived), I still get the same result. I am running this statement in the tinker:

App\MyModel::withoutGlobalScope(ArchiveScope::class)->get();

Strangely, when I use withoutGlobalScopes() instead of withoutGlobalScope(ArchiveScope::class) then I get all the records.

App\MyModel::withoutGlobalScopes()->get();

0 likes
5 replies
bobbybouwmann's avatar

Mmh that should actually work! Are you sure the namespace of the scope is correct? Is it imported the correct way?

Also how do you register your global scope? There are multiple ways for that as well!

tanmay_das's avatar

Hi @bobbybouwmann Thanks for your reply.

I followed the documentation step by step. Here is my scope that resides in app/Scopes directory:

<?php

namespace App\Scopes;

use Illuminate\Database\Eloquent\Scope;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Builder;

class ArchiveScope implements Scope
{
    /**
     * Apply the scope to a given Eloquent query builder.
     *
     * @param  \Illuminate\Database\Eloquent\Builder  $builder
     * @param  \Illuminate\Database\Eloquent\Model  $model
     * @return void
     */
    public function apply(Builder $builder, Model $model)
    {
        $builder->where('archived_at', '=', NULL);
    }
}

And this is how I am using it inside my model:

/**
     * The "booting" method of the model.
     *
     * @return void
     */
    protected static function boot()
    {
        parent::boot();
        static::addGlobalScope(new ArchiveScope);
    }

Also, I am using soft deletion for that model. Is there a possibility of conflict with laravels scope for soft deletion?

In the model, I am importing the scope like this:

use App\Scopes\ArchiveScope;
bobbybouwmann's avatar

Mmh this all looks correct. So yeah, it might be an issue with the soft delete, but that sounds strange to me! You can try it without the soft delete!

tanmay_das's avatar
tanmay_das
OP
Best Answer
Level 4

Turns out when I use the full class path, then it works:

App\MyModel::withoutGlobalScope('App\Scopes\ArchiveScope')->get();

Previously I have been using this:

App\MyModel::withoutGlobalScope(ArchiveScope::class)->get();

...as the documentation suggested.

I am using PHP 7.2.5.

bobbybouwmann's avatar

Mmmh, both should return the same string though... So that's why I asked about the namespace thing ;)

Please or to participate in this conversation.