Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork1
This Laravel package allows you to flip/invert an Eloquent scope, or really any query constraint.
License
protonemedia/laravel-eloquent-where-not
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
As of Laravel 9.2.0, you may use the nativewhereNot 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:https://youtu.be/fAY75SLQj3w
❤️ 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 considersponsoring the maintenance and development. Keeping track of issues and pull requests takes time, but we're happy to help!
Did you hear about Laravel Splade? 🤩
It's themagic of Inertia.js with thesimplicity of Blade.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.
- 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.
- Flip/invert your scope, or really any query constraint.
- Zero third-party dependencies.
Related package:Laravel Eloquent Scope as Select
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.
You can install the package via composer:
composer require protonemedia/laravel-eloquent-where-not
Add themacro to the query builder, for example, in yourAppServiceProvider. By default, the name of the macro iswhereNot, but you can customize it with the first parameter of theaddMacro method.
useProtoneMedia\LaravelEloquentWhereNot\WhereNot;publicfunctionboot(){ WhereNot::addMacro();// or use a custom method name: WhereNot::addMacro('not');}
For a more practical explanation, check out theusage section below.
Call thewhereNot 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();
Imagine you have aPost Eloquent model with a query scope that constraints the query to all posts that should make the front page.
class Postextends Model{publicfunctionuser() {return$this->belongsTo(User::class); }publicfunctioncomments() {return$this->hasMany(Comment::class); }publicfunctionscopeOnFrontPage($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 fetchall posts thatdidn'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();
Instead of using a Closure, there are some shortcuts you could use (see also: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']);
composertestPlease seeCHANGELOG for more information about what has changed recently.
Please seeCONTRIBUTING for details.
Laravel Analytics Event Tracking: Laravel package to easily send events to Google Analytics.Laravel Blade On Demand: Laravel package to compile Blade templates in memory.Laravel Cross Eloquent Search: Laravel package to search through multiple Eloquent models.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: This package provides an integration with FFmpeg for Laravel. The storage of the files is handled by Laravel's Filesystem.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: Paddle.com API integration for Laravel with support for webhooks/events.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: WebDAV driver for Laravel's Filesystem.
If you discover any security related issues, please emailpascal@protone.media instead of using the issue tracker.
The MIT License (MIT). Please seeLicense File for more information.
This package isTreeware. If you use it in production, then we ask that youbuy the world a tree to thank us for our work. By contributing to the Treeware forest you’ll be creating employment for local families and restoring wildlife habitats.
About
This Laravel package allows you to flip/invert an Eloquent scope, or really any query constraint.
Topics
Resources
License
Contributing
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Sponsor this project
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.