PHPackages                             omaressaouaf/query-builder-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. [Search &amp; Filtering](/categories/search)
4. /
5. omaressaouaf/query-builder-criteria

ActiveLibrary[Search &amp; Filtering](/categories/search)

omaressaouaf/query-builder-criteria
===================================

Define reusable query criteria for filtering, sorting, search, field selection, and includes in Laravel Eloquent models

1.0.0(1y ago)282.4k↑266.7%2MITPHPPHP ^8.2|^8.3|^8.4CI passing

Since Mar 20Pushed 1y ago2 watchersCompare

[ Source](https://github.com/omaressaouaf/query-builder-criteria)[ Packagist](https://packagist.org/packages/omaressaouaf/query-builder-criteria)[ Docs](https://github.com/omaressaouaf/query-builder-criteria)[ RSS](/packages/omaressaouaf-query-builder-criteria/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (1)Dependencies (5)Versions (2)Used By (0)

Query Builder Criteria
======================

[](#query-builder-criteria)

[![Latest Stable Version](https://camo.githubusercontent.com/0fefd798c1849b2896f81ada442a17d7531a7e80608dd7102d4f76a14bd62284/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6f6d6172657373616f7561662f71756572792d6275696c6465722d63726974657269612e737667)](https://packagist.org/packages/omaressaouaf/query-builder-criteria)[![License](https://camo.githubusercontent.com/00a44b41a01223ed024dad072a9f57003d4a66134f32b7ea83179baa1192d448/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f6f6d6172657373616f7561662f71756572792d6275696c6465722d6372697465726961)](LICENSE)[![Tests](https://github.com/omaressaouaf/query-builder-criteria/actions/workflows/tests.yml/badge.svg)](https://github.com/omaressaouaf/query-builder-criteria/actions/workflows/tests.yml)

### **Introduction:**

[](#introduction)

**Query Builder Criteria** is a Laravel package that extends [Spatie's Laravel Query Builder](https://spatie.be/docs/laravel-query-builder/v6/introduction), providing a structured way to define query filters, sorting, includes, and search functionality using reusable **criteria classes**.

With this package, you can:

- ✅ Define query logic in a **clean, structured** manner
- ✅ Apply **filters, sorts, includes, and field selections** effortlessly
- ✅ Support **search** and **full-text search** and **custom query aliases**
- ✅ **Merge multiple criteria dynamically** for flexible querying

Built on top of Spatie’s Query Builder, this package **removes repetitive query logic**, keeping your controllers and models **clean and maintainable**. 🚀

**🔗 Example:**

Instead of manually handling query parameters, just define a **Criteria Class**:

```
class PostCriteria extends Criteria
{
    protected array $filters = ['title', 'slug', 'user'];

    protected array $sorts = ['published_at', 'created_at'];

    protected array $includes = ['user'];
}
```

And apply it with **one line of code**:

```
$posts = Post::query()->queryByCriteria(PostCriteria::class)->get();
```

✨ No more manual query handling – just **define once and reuse**!

Table of Contents
-----------------

[](#table-of-contents)

- [Installation](#installation)
- [Get Started](#get-started)
- [Criteria configuration](#criteria-configuration)
    - [Filters](#filters)
    - [Sorting](#sorting)
    - [Includes](#includes)
    - [Field Selection](#field-selection)
    - [Search](#search)
    - [Aliases](#aliases)
    - [Advanced Features](#advanced-features)
- [Applying Criteria to a Model](#applying-criteria-to-a-model)
- [Querying Data](#querying-data)
- [Passing Criteria to Scope](#passing-criteria-to-scope)
- [Credits](#credits)
- [License](#license)

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

[](#installation)

Install via Composer:

```
composer require omaressaouaf/query-builder-criteria
```

### **Publishing the Configuration**

[](#publishing-the-configuration)

After installation, you can publish the package configuration file using:

```
php artisan vendor:publish --provider="Omaressaouaf\QueryBuilderCriteria\QueryBuilderCriteriaServiceProvider"
```

This will create a configuration file in `config/query-builder-criteria.php`, allowing you to customize the default behavior.

```
