PHPackages                             protonemedia/laravel-eloquent-where-not - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. protonemedia/laravel-eloquent-where-not

AbandonedArchivedLibrary[Database &amp; ORM](/categories/database)

protonemedia/laravel-eloquent-where-not
=======================================

Laravel package to invert Eloquent scopes.

1.2.0(4y ago)2644.6k↓25.6%11MITPHPPHP ^7.4|^8.0|^8.1

Since Dec 9Pushed 3y ago3 watchersCompare

[ Source](https://github.com/protonemedia/laravel-eloquent-where-not)[ Packagist](https://packagist.org/packages/protonemedia/laravel-eloquent-where-not)[ Docs](https://github.com/protonemedia/laravel-eloquent-where-not)[ GitHub Sponsors](https://github.com/pascalbaljet)[ RSS](/packages/protonemedia-laravel-eloquent-where-not/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (4)Dependencies (4)Versions (8)Used By (1)

Laravel Eloquent Where Not
==========================

[](#laravel-eloquent-where-not)

[![Latest Version on Packagist](https://camo.githubusercontent.com/f83a7e149a96bc0e5c5d65ebfba045b29e8813ffdeafb6a663a6811b37c0fa8c/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f70726f746f6e656d656469612f6c61726176656c2d656c6f7175656e742d77686572652d6e6f742e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/protonemedia/laravel-eloquent-where-not)[![run-tests](https://github.com/protonemedia/laravel-eloquent-where-not/workflows/run-tests/badge.svg)](https://github.com/protonemedia/laravel-eloquent-where-not/workflows/run-tests/badge.svg)[![Quality Score](https://camo.githubusercontent.com/7ec1fa485643e14a9d7ec123a4857b29c9ad98ade714b38b95076963d2c0e53e/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f70726f746f6e656d656469612f6c61726176656c2d656c6f7175656e742d77686572652d6e6f742e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/protonemedia/laravel-eloquent-where-not)[![Total Downloads](https://camo.githubusercontent.com/cc2268aa5c9a05aa39a8a3ff4376839218c1df15bfd9271f3c42d7109f13dbde/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f70726f746f6e656d656469612f6c61726176656c2d656c6f7175656e742d77686572652d6e6f742e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/protonemedia/laravel-eloquent-where-not)[![Buy us a tree](https://camo.githubusercontent.com/130148911f548b001b2ac68a32c0a06559977ca60ada3bf480c72ae4ea093175/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f54726565776172652d2546302539462538432542332d6c69676874677265656e)](https://plant.treeware.earth/protonemedia/laravel-eloquent-where-not)

### Included in Laravel 9.2.0!

[](#included-in-laravel-920)

As of Laravel 9.2.0, you may use the native [`whereNot`](https://laravel.com/docs/9.x/queries#where-not-clauses) method. This package will be maintained for some time for Laravel 8 users but will be deprecated eventually.

### 📺 Want to see this package in action? Join the live stream on December 10 at 13:30 CET:

[](#-want-to-see-this-package-in-action-join-the-live-stream-on-december-10-at-1330-cet-httpsyoutubefay75slqj3w)

Support this package!
---------------------

[](#support-this-package)

❤️ We proudly support the community by developing Laravel packages and giving them away for free. If this package saves you time or if you're relying on it professionally, please consider [sponsoring the maintenance and development](https://github.com/sponsors/pascalbaljet). Keeping track of issues and pull requests takes time, but we're happy to help!

Laravel Splade
--------------

[](#laravel-splade)

**Did you hear about Laravel Splade? 🤩**

It's the *magic* of Inertia.js with the *simplicity* of Blade. [Splade](https://github.com/protonemedia/laravel-splade) provides a super easy way to build Single Page Applications using Blade templates. Besides that magic SPA-feeling, it comes with more than ten components to sparkle your app and make it interactive, all without ever leaving Blade.

Requirements
------------

[](#requirements)

- PHP 7.4+
- Laravel 8.0 or 9.0

This package is tested with GitHub Actions using MySQL 5.7, PostgreSQL 10.8 and SQLite.

Features
--------

[](#features)

- Flip/invert your scope, or really any query constraint.
- Zero third-party dependencies.

Related package: [Laravel Eloquent Scope as Select](https://github.com/protonemedia/laravel-eloquent-scope-as-select)

Blogpost
--------

[](#blogpost)

If you want to know more about the background of this package, please read the blogpost: [Apply the opposite of your Eloquent scope to the Query Builder with a Laravel package](https://protone.media/blog/apply-the-opposite-of-your-eloquent-scope-to-the-query-builder-with-a-laravel-package).

Installation
------------

[](#installation)

You can install the package via composer:

```
composer require protonemedia/laravel-eloquent-where-not
```

Add the `macro` to the query builder, for example, in your `AppServiceProvider`. By default, the name of the macro is `whereNot`, but you can customize it with the first parameter of the `addMacro` method.

```
use ProtoneMedia\LaravelEloquentWhereNot\WhereNot;

public function boot()
{
    WhereNot::addMacro();

    // or use a custom method name:
    WhereNot::addMacro('not');
}
```

Short API description
---------------------

[](#short-api-description)

*For a more practical explanation, check out the [usage](#usage) section below.*

Call the `whereNot` method with a Closure:

```
Post::whereNot(function ($query) {
    $query->onFrontPage();
})->get();
```

The example above can be shortened by using a string, which should be the name of the scope:

```
Post::whereNot('onFrontPage')->get();
```

You can use an array to call multiple scopes:

```
Post::whereNot(['popular', 'published'])->get();
```

Use an associative array to call dynamic scopes:

```
Post::whereNot(['ofType' => 'announcement'])->get();
```

If your dynamic scopes require multiple arguments, you can use an associative array:

```
Post::whereNot(['publishedBetween' => [2010, 2020]])->get();
```

You can also mix dynamic and non-dynmaic scopes:

```
Post::whereNot([
    'published',
    'ofType' => 'announcement'
])->get();
```

Usage
-----

[](#usage)

Imagine you have a `Post` Eloquent model with a query scope that constraints the query to all posts that should make the front page.

```
class Post extends Model
{
    public function user()
    {
        return $this->belongsTo(User::class);
    }

    public function comments()
    {
        return $this->hasMany(Comment::class);
    }

    public function scopeOnFrontPage($query)
    {
        $query->where('is_public', 1)
            ->where('votes', '>', 100)
            ->has('comments', '>=', 20)
            ->whereHas('user', fn($user) => $user->isAdmin())
            ->whereYear('published_at', date('Y'));
    }
}
```

Now you can fetch all posts for your front page by calling the scope method on the query:

```
$posts = Post::onFrontPage()->get();
```

But what if you want to fetch *all* posts that *didn't* make the front page? Using the power of this package, you can re-use your scope!

```
$posts = Post::whereNot(function($query) {
    $query->onFrontPage();
})->get();
```

With short closures, a feature which was introduced in PHP 7.4, this can be even shorter:

```
$posts = Post::whereNot(fn ($query) => $query->onFrontPage())->get();
```

### Shortcuts

[](#shortcuts)

Instead of using a Closure, there are some shortcuts you could use (see also: [Short API description](#short-api-description)):

Using a string instead of a Closure:

```
Post::whereNot(function ($query) {
    $query->published();
});

// is the same as:

Post::whereNot('published');
```

Using an array instead of Closure, to support multiple scopes and dynamic scopes:

```
Post::whereNot(function ($query) {
    $query->ofType('announcement');
});

// is the same as:

Post::whereNot(['ofType' => 'announcement']);
```

### Testing

[](#testing)

```
composer test
```

### Changelog

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information about what has changed recently.

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

Other Laravel packages
----------------------

[](#other-laravel-packages)

- [`Laravel Analytics Event Tracking`](https://github.com/protonemedia/laravel-analytics-event-tracking): Laravel package to easily send events to Google Analytics.
- [`Laravel Blade On Demand`](https://github.com/protonemedia/laravel-blade-on-demand): Laravel package to compile Blade templates in memory.
- [`Laravel Cross Eloquent Search`](https://github.com/protonemedia/laravel-cross-eloquent-search): Laravel package to search through multiple Eloquent models.
- [`Laravel Eloquent Scope as Select`](https://github.com/protonemedia/laravel-eloquent-scope-as-select): Stop duplicating your Eloquent query scopes and constraints in PHP. This package lets you re-use your query scopes and constraints by adding them as a subquery.
- [`Laravel FFMpeg`](https://github.com/protonemedia/laravel-ffmpeg): This package provides an integration with FFmpeg for Laravel. The storage of the files is handled by Laravel's Filesystem.
- [`Laravel Form Components`](https://github.com/protonemedia/laravel-form-components): Blade components to rapidly build forms with Tailwind CSS Custom Forms and Bootstrap 4. Supports validation, model binding, default values, translations, includes default vendor styling and fully customizable!
- [`Laravel Paddle`](https://github.com/protonemedia/laravel-paddle): Paddle.com API integration for Laravel with support for webhooks/events.
- [`Laravel Verify New Email`](https://github.com/protonemedia/laravel-verify-new-email): This package adds support for verifying new email addresses: when a user updates its email address, it won't replace the old one until the new one is verified.
- [`Laravel WebDAV`](https://github.com/protonemedia/laravel-webdav): WebDAV driver for Laravel's Filesystem.

### Security

[](#security)

If you discover any security related issues, please email  instead of using the issue tracker.

Credits
-------

[](#credits)

- [Pascal Baljet](https://github.com/protonemedia)
- [All Contributors](../../contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

Treeware
--------

[](#treeware)

This package is [Treeware](https://treeware.earth). If you use it in production, then we ask that you [**buy the world a tree**](https://plant.treeware.earth/pascalbaljetmedia/laravel-eloquent-where-not) to thank us for our work. By contributing to the Treeware forest you’ll be creating employment for local families and restoring wildlife habitats.

###  Health Score

38

—

LowBetter than 85% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity38

Limited adoption so far

Community12

Small or concentrated contributor base

Maturity67

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~140 days

Total

4

Last Release

1565d ago

PHP version history (2 changes)1.0.0PHP ^7.4|^8.0

1.1.0PHP ^7.4|^8.0|^8.1

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/8403149?v=4)[Pascal Baljet](/maintainers/pascalbaljet)[@pascalbaljet](https://github.com/pascalbaljet)

---

Top Contributors

[![pascalbaljet](https://avatars.githubusercontent.com/u/8403149?v=4)](https://github.com/pascalbaljet "pascalbaljet (16 commits)")

---

Tags

eloquentlaravellaravel-packagesphpquery-builderquery-constraintprotonemedialaravel-eloquent-where-not

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/protonemedia-laravel-eloquent-where-not/health.svg)

```
[![Health](https://phpackages.com/badges/protonemedia-laravel-eloquent-where-not/health.svg)](https://phpackages.com/packages/protonemedia-laravel-eloquent-where-not)
```

###  Alternatives

[illuminate/database

The Illuminate Database package.

2.8k52.4M9.4k](/packages/illuminate-database)[cviebrock/eloquent-taggable

Easy ability to tag your Eloquent models in Laravel.

567694.8k3](/packages/cviebrock-eloquent-taggable)[clickbar/laravel-magellan

This package provides functionality for working with the postgis extension in Laravel.

423715.4k1](/packages/clickbar-laravel-magellan)[genealabs/laravel-pivot-events

This package introduces new eloquent events for sync(), attach(), detach() or updateExistingPivot() methods on BelongsToMany relation.

1404.9M8](/packages/genealabs-laravel-pivot-events)[protonemedia/laravel-eloquent-scope-as-select

Laravel package to add Eloquent scopes as constraints as subquery selects.

144497.9k](/packages/protonemedia-laravel-eloquent-scope-as-select)[reedware/laravel-relation-joins

Adds the ability to join on a relationship by name.

2121.2M13](/packages/reedware-laravel-relation-joins)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
