Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Dec 9, 2022. It is now read-only.

This Laravel package allows you to flip/invert an Eloquent scope, or really any query constraint.

License

NotificationsYou must be signed in to change notification settings

protonemedia/laravel-eloquent-where-not

Repository files navigation

Latest Version on Packagistrun-testsQuality ScoreTotal DownloadsBuy us a tree

Included in Laravel 9.2.0!

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

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 considersponsoring the maintenance and development. Keeping track of issues and pull requests takes time, but we're happy to help!

Laravel Splade

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.

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

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

Related package:Laravel Eloquent Scope as Select

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.

Installation

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');}

Short API description

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

Usage

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

Shortcuts

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']);

Testing

composertest

Changelog

Please seeCHANGELOG for more information about what has changed recently.

Contributing

Please seeCONTRIBUTING for details.

Other Laravel packages

  • 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.

Security

If you discover any security related issues, please emailpascal@protone.media instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please seeLicense File for more information.

Treeware

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.

Sponsor this project

 

Languages


[8]ページ先頭

©2009-2025 Movatter.jp