PHPackages                             alex-storozhenko/eloquent-criteria - 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. alex-storozhenko/eloquent-criteria

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

alex-storozhenko/eloquent-criteria
==================================

Lightweight Criteria Builder implementation for Eloquent

v2.0.0(2y ago)113[2 PRs](https://github.com/alex-storozhenko/eloquent-criteria/pulls)MITPHPPHP ^8.1

Since Mar 7Pushed 2y ago2 watchersCompare

[ Source](https://github.com/alex-storozhenko/eloquent-criteria)[ Packagist](https://packagist.org/packages/alex-storozhenko/eloquent-criteria)[ Docs](https://github.com/alex-storozhenko/eloquent-criteria)[ RSS](/packages/alex-storozhenko-eloquent-criteria/feed)WikiDiscussions main Synced today

READMEChangelog (4)Dependencies (14)Versions (7)Used By (0)

[![Eloquent Criteria like LEGO® for Eloquent Builder](./art/banner.png)](./art/banner.png)

---

Eloquent Criteria
=================

[](#eloquent-criteria)

[![Latest Version on Packagist](https://camo.githubusercontent.com/530b2014fb6ad90b3964f03a4330d74b0b742c9418e26c4d8cf73d70d46bfa89/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f616c65782d73746f726f7a68656e6b6f2f656c6f7175656e742d63726974657269612e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/alex-storozhenko/eloquent-criteria)[![GitHub Tests Action Status](https://camo.githubusercontent.com/f4d42bbecde10ccd98da7c8f15d7a165b153961efcdcb0f0f52f351270f9b5d9/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f616c65782d73746f726f7a68656e6b6f2f656c6f7175656e742d63726974657269612f72756e2d74657374732e796d6c3f6272616e63683d6d61696e266c6162656c3d7465737473267374796c653d666c61742d737175617265)](https://github.com/alex-storozhenko/eloquent-criteria/actions?query=workflow%3Arun-tests+branch%3Amain)[![GitHub Code Style Action Status](https://camo.githubusercontent.com/61654f643b645ec13f7d5a59df4e23d056c452f54a4b20b2c8efdca17fa48769/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f616c65782d73746f726f7a68656e6b6f2f656c6f7175656e742d63726974657269612f6669782d7068702d636f64652d7374796c652d6973737565732e796d6c3f6272616e63683d6d61696e266c6162656c3d636f64652532307374796c65267374796c653d666c61742d737175617265)](https://github.com/alex-storozhenko/eloquent-criteria/actions?query=workflow%3A%22Fix+PHP+code+style+issues%22+branch%3Amain)[![Total Downloads](https://camo.githubusercontent.com/6f46acfdc9ae4f0e6dc45820f3af64ddd891ed03653ae19e1c83215e714726d9/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f616c65782d73746f726f7a68656e6b6f2f656c6f7175656e742d63726974657269612e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/alex-storozhenko/eloquent-criteria)

### ⚠️ Compatibility Notes:

[](#️-compatibility-notes)

Please use 1.x.x version for Laravel 9 compatibility:

```
composer require alex-storozhenko/eloquent-criteria ^1.1
```

This is a lightweight implementation of Criteria Builder for Eloquent. It's like LEGO® for Eloquent Builder, providing easy decoupling and reuse of query modifiers.

Obviously, the main purpose is to modify the query in the specified way, since Criteria are very similar to Eloquent scopes, only with that they can be applied in different way in a app and encapsulate more complex conditions with additional logic in a pretty clean way.

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

[](#installation)

You can install the package via composer:

```
composer require alex-storozhenko/eloquent-criteria
```

You can publish the config file with:

```
php artisan vendor:publish --tag="eloquent-criteria-config"
```

This is the contents of the published config file:

```
return [
    /*
    |--------------------------------------------------------------------------
    | Macro
    |--------------------------------------------------------------------------
    |
    | This option controls the ability to add a macro to the eloquent builder,
    | which allows, when enabled, to add criteria builder functionality to the eloquent
    | at the global level
    | methods criteriaQuery(), apply() will be added
    | to the Eloquent through the macro
    |
    */
    'macro_enabled' => env('ELOQUENT_CRITERIA_MACRO_ENABLED', false),
];
```

Usage
-----

[](#usage)

There are several ways to get started with the Criteria Builder, one of which is to simply enable the macro and make it work globally, just add it to your .env file

### Macro

[](#macro)

```
ELOQUENT_CRITERIA_MACRO_ENABLED=true
```

or change a value in config `eloquent-criteria.php`

and let's do magic:

### Criteria class

[](#criteria-class)

```
