New

Just launched: Vueblocks, a brand-new search engine for Vue packages! Explore Vueblocks →

baethon/laravel-criteria

Minimal interface for criteria pattern
7,485 9
Install
composer require baethon/laravel-criteria
Latest Version:1.1.0
PHP:>=8.1
License:MIT
Last Updated:Jan 9, 2025
Links: GitHub  ·  Packagist
Maintainer: radmen

baethon/laravel-criteria

Minimal implementation of criteria pattern for Laravel.

What is criteria?

Criteria is an implementation of \Baethon\LaravelCriteria\CriteriaInterface which is responsible for only one thing - modify Laravels query in a specified way.

This allows to decouple and re-use query modifiers.

Criteria are very similar to Eloquent scopes. The main difference is that they can be applied to different models across the application.

Installation

composer require baethon/laravel-criteria

Requirements

  • PHP >= 7.1

Laravel compatibility

This library has no dependencies to Laravel itself. It should work with all versions of Laravel.

Usage

First, you need to create an implementation of CriteriaInterface:

<?php

use Baethon\LaravelCriteria\CriteriaInterface;

class CompareCriteria implements CriteriaInterface
{
    private $field;

    private $value;

    public function __construct(string $field, string $value)
    {
        $this->field = $field;
        $this->value = $value;
    }

    public function apply($query)
    {
        $query->where($this->field, $this->value);
    }
}

Now, you can apply it to the query:

$query = User::query();

(new CompareCriteria('name', 'Jon'))->apply($query);

$jon = $query->first();

AppliesCriteria trait

To simplify things it's possible to use AppliesCriteria trait in a model.

use Baethon\LaravelCriteria\Traits\AppliesCriteria;

class User extends Model
{
    // model body stripped for better readability

    use AppliesCriteria;
}

$jon = User::query()
    ->apply(new CompareCriteria('name', 'Jon'))
    ->first();

Collections

The package provides collections which allow applying multiple criteria at once.

To apply group of criteria use \Baethon\LaravelCriteria\Collections\CriteriaCollection:

$jonSnow = User::query()
    ->apply(CriteriaCollection::create([
        new CompareCriteria('name', 'Jon'),
        new CompareCriteria('lastname', 'Snow'),
    ]))
    ->first();

// same result, without CriteriaCollection
$jonSnow = User::query()
    ->apply(new CompareCriteria('name', 'Jon'))
    ->apply(new CompareCriteria('lastname', 'Snow'))
    ->first();

If you need to be sure that all criteria will be fulfilled you can use CriteriaCollection::allOf():

User::query()
    ->apply(CriteriaCollection::allOf([
        new CompareCriteria('name', 'Jon'),
        new CompareCriteria('lastname', 'Snow'),
    ]));

// same as
User::query()
    ->where(function ($query) {
        $query->apply(new CompareCriteria('name', 'Jon'))
            ->apply(new CompareCriteria('lastname', 'Snow'));
    });

Also, you can group criteria using logical OR join:

User::query()
    ->apply(CriteriaCollection::oneOf([
        new CompareCriteria('name', 'Jon'),
        new CompareCriteria('lastname', 'Snow'),
    ]));

// same as
User::query()
    ->where(function ($query) {
        $query->where('name', 'Jon')
            ->orWhere('lastname', 'Snow');
    });

Testing

Run tests with:

./vendor/bin/phpunit

Changelog

Please see CHANGELOG for more information what has changed recently.

License

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

Related Packages

doctrine/dbal

Powerful PHP database abstraction layer (DBAL) with many features for database s...

651,963,193 9,701
laravel/framework

The Laravel Framework.

604,216,237 34,982
laravel/tinker

Powerful REPL for the Laravel framework.

518,004,183 7,444
laravel/serializable-closure

Laravel Serializable Closure provides an easy and secure way to serialize closur...

435,097,283 607
nunomaduro/collision

Cli error handling for console/command-line PHP applications.

407,699,310 4,663

Version History

Version Released PHP License
1.1.0 >=8.1 MIT
1.0.2 >=7.1 MIT
1.0.1 >=7.1 MIT
1.0.0 MIT