Eloquent Global Scopes: Pitfalls &amp; Safe Composition | Mohamed Said       [Skip to content](#main)  [ ![](https://cdn.msaied.com/01KT78WE565VEMM3PSNQAAB0MH.png) Mohamed SaidLaravel Backend Engineer ](https://www.msaied.com/public) - [Home](https://www.msaied.com/public)
- [Projects](https://www.msaied.com/public/projects)
- [Articles](https://www.msaied.com/public/articles)
- [Certificates](https://www.msaied.com/public/certificates)
- [About](https://www.msaied.com/public#about)

           [  Contact](https://www.msaied.com/public#contact) Menu 

Menu
----

Close 

 - [HomeStart here](https://www.msaied.com/public)
- [ProjectsCase studies](https://www.msaied.com/public/projects)
- [ArticlesEngineering notes](https://www.msaied.com/public/articles)
- [CertificatesCredentials](https://www.msaied.com/public/certificates)
- [AboutHow I work](https://www.msaied.com/public#about)
- [ContactGet in touch](https://www.msaied.com/public#contact)

  [Start a conversation](https://www.msaied.com/public#contact) [WhatsApp](https://wa.me/201094619204) [Email](mailto:hello@msaied.com) 

 1. [Home](https://www.msaied.com/public)
2. /
3. [Articles](https://www.msaied.com/public/articles)
4. /
5. Laravel Eloquent Global Scopes: Pitfalls, Testing, and Composing Them Safely

 Laravel Eloquent Global Scopes: Pitfalls, Testing, and Composing Them Safely
=============================================================================

 Global scopes are powerful but easy to misuse. Learn how to write, test, and safely compose Eloquent global scopes without leaking state or breaking queries in unexpected ways.

 ![](https://cdn.msaied.com/01M22N44A70A5MC2S599JP0MPH.webp) [Mohamed Said](https://www.msaied.com/public#person) Published 16 Jun 2026 · Updated 16 Jun 2026 · 1 min read

ShareCopy linkCopied

 ![Laravel Eloquent Global Scopes: Pitfalls, Testing, and Composing Them Safely](https://cdn.msaied.com/211/8b9b19e7ecbf690b182ffbe6bffc9530.png) 

  On this page +1. [Eloquent Global Scopes: Pitfalls, Testing, and Composing Them Safely](#eloquent-global-scopes-pitfalls-testing-and-composing-them-safely)
2. [How Global Scopes Actually Attach to Queries](#how-global-scopes-actually-attach-to-queries)
3. [The Multi-Scope Conflict Problem](#the-multi-scope-conflict-problem)
4. [Scopes That Touch Joins](#scopes-that-touch-joins)
5. [Testing Global Scopes with Pest](#testing-global-scopes-with-pest)
6. [Composing Scopes Without Surprises](#composing-scopes-without-surprises)
7. [Takeaways](#takeaways)

 Eloquent Global Scopes: Pitfalls, Testing, and Composing Them Safely
--------------------------------------------------------------------

Global scopes are one of Eloquent's most useful — and most quietly dangerous — features. Applied correctly they eliminate repetitive `where` clauses across an entire codebase. Applied carelessly they produce queries that silently exclude data, break eager loads, or conflict with one another in ways that only surface under production load.

This article focuses on the practical problems that appear once you have more than one global scope on a model, and how to handle them deliberately.

### How Global Scopes Actually Attach to Queries

Every time Eloquent builds a new `Builder` instance for a model it calls `registerGlobalScopes`, which iterates the scopes registered via `static::addGlobalScope()` in `booted()` and calls `$builder->withGlobalScope($identifier, $scope)`. The identifier is the key used later by `withoutGlobalScope()`.

```php
class PublishedScope implements Scope
{
    public function apply(Builder $builder, Model $model): void
    {
        $builder->where('published_at', '
