Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

laravel

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) I get all the records.

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

like image 869
Tanmay Avatar asked Jun 06 '18 06:06

Tanmay


1 Answers

Defining the full class path solves the problem:

App\MyModel::withoutGlobalScope('App\Scopes\ArchiveScope')->get();
like image 154
Tanmay Avatar answered Sep 18 '22 00:09

Tanmay