Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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();

As with other here I solved this problem by using the fullnamespace, however for a long time it was not obvious to me i was not.

I was using

StoreItem::withoutGlobalScope(App\Scopes\CompletedStoreItems::class)->where('id',$id)->get();

if I copied and pasted this into tinker it worked, but would not work in my code. It turns out that because I did not have a \ before App it was actually looking for

App\Models\App\Scopes\CompletedStoreItems

which is wrong and quite difficult to debug.

Make sure you put the \ before App

ps. Tanmay's code above works because he is using a string.

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.