Download the PHP package protonemedia/laravel-eloquent-scope-as-select without Composer
On this page you can find all versions of the php package protonemedia/laravel-eloquent-scope-as-select. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download protonemedia/laravel-eloquent-scope-as-select
More information about protonemedia/laravel-eloquent-scope-as-select
Files in protonemedia/laravel-eloquent-scope-as-select
Package laravel-eloquent-scope-as-select
Short Description Laravel package to add Eloquent scopes as constraints as subquery selects.
License MIT
Homepage https://github.com/protonemedia/laravel-eloquent-scope-as-select
Informations about the package laravel-eloquent-scope-as-select
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.
📺 Want to see this package in action? Join the live stream on December 3 at 14:00 CET: https://youtu.be/0vR8IQSFsfQ
Requirements
- PHP 8.2+
- Laravel 10.0
This package is tested with GitHub Actions using MySQL 8.0, PostgreSQL 10.8 and SQLite.
Features
- Add a subquery based on a query scope.
- Add a subquery using a Closure.
- Shortcuts for calling scopes by using a string or array.
- Support for more than one subquery.
- Support for flipping the result.
- Zero third-party dependencies.
Related package: Laravel Eloquent Where Not
Sponsor Us
❤️ 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 and check out our latest premium package: Inertia Table. Keeping track of issues and pull requests takes time, but we're happy to help!
Blogpost
If you want to know more about the background of this package, please read the blogpost: Stop duplicating your Eloquent query scopes and constraints. Re-use them as select statements with a new Laravel package.
Installation
You can install the package via composer:
Add the macro
to the query builder, for example, in your AppServiceProvider
. By default, the name of the macro is addScopeAsSelect
, but you can customize it with the first parameter of the addMacro
method.
Short API description
For a more practical explanation, check out the usage section below.
Add a select using a Closure. Each Post
model, published or not, will have an is_published
attribute.
The example above can be shortened by using a string, where the second argument is the name of the scope:
You can use an array to call multiple scopes:
Use an associative array to call dynamic scopes:
If your dynamic scopes require multiple arguments, you can use an associative array:
You can also mix dynamic and non-dynmaic scopes:
The method has an optional third argument that flips the result.
Usage
Imagine you have a Post
Eloquent model with a query scope.
Now you can fetch all published posts by calling the scope method on the query:
But what if you want to fetch all posts and then check if the post is published? This scope is quite simple, so you can easily mimic the scope's outcome by checking the published_at
attribute:
This is harder to achieve when scopes get more complicated or when you chain various scopes. Let's add a relationship and another scope to the Post
model:
Using Eloquent, we can fetch all posts from this year with at least ten comments.
Great! Now we want to fetch all posts again, and then check if the post was published this year and has at least ten comments.
Well, you get the idea. This is bound to get messy and you're duplicating logic as well.
Solution
Using the power of this package, you can re-use your scopes when fetching data. The first example (published
scope) can be narrowed down to:
With short closures, a feature which was introduced in PHP 7.4, this can be even shorter:
Now every Post
model will have an is_published
boolean attribute.
You can add multiple selects as well, for example, to combine both scenarios:
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:
Using an array instead of Closure, to support multiple scopes and dynamic scopes:
You can also flip the result with the optional third parameter (it defaults to true
):
Testing
Changelog
Please see CHANGELOG for more information about what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Other Laravel packages
Inertia Table
: The Ultimate Table for Inertia.js with built-in Query Builder.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 FFMpeg
: This package provides an integration with FFmpeg for Laravel. The storage of the files is handled by Laravel's Filesystem.Laravel MinIO Testing Tools
: Run your tests against a MinIO S3 server.Laravel Mixins
: A collection of Laravel goodies.Laravel Paddle
: Paddle.com API integration for Laravel with support for webhooks/events.Laravel Task Runner
: Write Shell scripts like Blade Components and run them locally or on a remote server.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 XSS Protection
: Laravel Middleware to protect your app against Cross-site scripting (XSS). It sanitizes request input, and it can sanatize Blade echo statements.
Security
If you discover any security related issues, please email [email protected] instead of using the issue tracker.
Credits
- Pascal Baljet
- All Contributors
License
The MIT License (MIT). Please see License File for more information.
Treeware
This package is Treeware. If you use it in production, then we ask that you buy 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.