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.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package laravel-eloquent-scope-as-select

Laravel Eloquent Scope as Select

Latest Version on Packagist run-tests Quality Score Total Downloads Buy us a tree

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

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

Features

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

Security

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

Credits

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.


All versions of laravel-eloquent-scope-as-select with dependencies

PHP Build Version
Package Version
Requires php Version ^8.2|^8.3|^8.4
illuminate/support Version ^10.0|^11.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package protonemedia/laravel-eloquent-scope-as-select contains the following files

Loading the files please wait ....