Download the PHP package architools/laravel-sieve without Composer

On this page you can find all versions of the php package architools/laravel-sieve. 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-sieve

Introduction

Laravel Sieve is a modular, scalable filtering engine designed to keep your codebase clean, maintainable, and easy to extend. It eliminates bloated controllers and scattered if statements with a structured, composable approach to dynamic, request-driven query filtering. By isolating filter logic into well-defined, single-responsibility classes, it enforces true separation of concerns while staying fully aligned with SOLID and DRY principles.

Whether you're building RESTful APIs, admin panels, or data-intensive applications, Laravel Sieve fits right in β€” offering the flexibility and structure needed for modern Laravel projects:

🧱 Decouple business logic with focused, single-responsibility filter classes.

πŸ”„ Easily extend and customize filters to meet your unique requirements.

🧠 Compose complex query conditions effortlessly β€” even across nested relationships

↕️ Apply multiple sorts dynamically and cleanly, directly from request input

βš™οΈ Seamlessly integrate into your existing Laravel codebase with minimal setup

Designed for scalability, and developer satisfaction.

Requirements

Installation

  1. Install the package via Composer:

Table of Contents


Getting Started

Create a Custom Utilities Service

To begin, define your own service class that extends the base UtilitiesService class:


Defining Filters

Define available filters inside the filters() method of your service class. Each filter is a key-value pair where:

Each filter method receives two arguments:

Criteria $criteria: The criteria instance for appending conditions & joins.

mixed $value: The filter value from the request.

πŸ”Ž For more on how Criteria works, see the Criteria section

πŸ”’ Tip: Always validate filter values using Form Requests, or another approach you prefer.

πŸ“˜ Explore Available Conditions β†’

Applying Joins in Filters

Need to use Join's in filters? No problem β€” just append them inside your filter method:

You can even attach conditions directly to the join:

βœ… Best Practice: To avoid overwriting joins when reusing the same one in multiple filters, always check if it already exists:

πŸ”’ Important: Appending a join with an existing name will overwrite the previous one.

Example with Multiple Filters Using Joins

πŸ“˜ Explore Available Joins β†’


Reusable Filter Classes

You can reuse common filters across services by implementing the Filter interface:

To apply a reusable filter class in your service, simply register it in the filters() method:

πŸ“Œ Note: If you need to reuse the same filter logic but apply it to different columns, consider modifying the filter class to accept a column name and use it dynamically in the apply() method.

You can reuse it in other services too:

πŸ”’ Important: πŸ“Œ Make sure your filter class implements the Filter interface and its apply() method.

Defining Sorts

Define available sorts inside the sorts() method of your service class. Each sort is a key-value pair where:

πŸ”’ Note: Sorts query parameters are expected in the following format:

πŸ“˜ Explore Available Sorts β†’


Using the Utilities Service

Inject the service into your controller and apply filters and sorts:


Building the Query

Utilize the Criteria object to modify thebuilder instance

βœ… applyOnBuilder() can be used anywhere you're building queriesβ€”not just limited repositories.

Components

Utilities Service

The UtilitiesService class is an abstract base class that provides a structured way to handle filtering and sorting in your Laravel applications. It acts as a bridge between HTTP requests and the Criteria class, making it easy to implement filtering and sorting functionality in your services.

Purpose

The UtilitiesService class serves as a foundation for building filterable and sortable services. It allows you to:

Available Methods

getCriteria(): Criteria

fresh(): UtilitiesService

Initializes a fresh Criteria instance and resets the internal state of the service.

applyFilters(): UtilitiesService

Applies all valid filters from the request to the Criteria instance.

applySorts(): UtilitiesService

Applies all valid sorts from the request to the Criteria instance.

Protected Methods to Override

filters(): array

Define the available filters for your service.

sorts(): array

Define the available sorts for your service.

Usage Examples

Basic Implementation

Complex Implementation

Best Practices


Criteria

The Criteria class is the main orchestrator of the Laravel Sieve package. It manages and applies joins, conditions, and sorts to your query builder in a structured and organized way.

Purpose

The Criteria class serves as a container and manager for all query modifications. It allows you to:

Available Methods

appendJoin(BaseJoin $join, int $sort = 100): Criteria

Adds a join to the criteria with an optional sort order.

appendSort(BaseSort $sort): Criteria

Adds a sort to the criteria.

removeJoinIfExists(string $joinName): Criteria

Removes a join from the criteria if it exists.

joinExists(string $joinName): bool

Checks if a join exists in the criteria.

appendCondition(BaseCondition $condition): Criteria

Adds a condition to the criteria.

applyOnBuilder(Builder $builder): Builder

Applies all joins, conditions, and sorts to the query builder in the correct order.

Example


Conditions

Basic Conditions

Condition

Purpose: The fundamental building block for creating WHERE clauses in your queries. It handles basic comparison operations between a field and a value.

Parameters:

Example:

ColumnCondition

Purpose: Used when you need to compare two columns in the same table or across joined tables.

Parameters:

Example:

JSON Conditions

JsonContainCondition

Parameters:

Purpose: Checks if a JSON array contains a specific value. Useful for querying JSON columns that store arrays.

Parameters:

Example:

JsonContainsKeyCondition

Purpose: Verifies if a JSON object contains a specific key. Useful for checking the existence of properties in JSON data.

Parameters:

Example:

JsonLengthCondition

Purpose: Compares the length of a JSON array. Useful for filtering based on array size.

Parameters:

Example:

JsonOverlapCondition

Purpose: Checks if two JSON arrays have any elements in common. Useful for finding records with matching array elements.

Parameters:

Example:

Aggregation Conditions

AggregationCondition

Purpose: Applies conditions on aggregated values (COUNT, SUM, AVG, etc.). Essential for filtering based on grouped data.

Parameters:

Example:

Group Conditions

GroupConditions

Purpose: Groups multiple conditions together with a logical operator (AND/OR). Enables complex query composition.

Parameters:

Example:

Note

The GroupConditions class does not support mixing different condition types. Specifically, you cannot combine AggregationCondition instances with standard (non-aggregation) Condition instances in the same group.

If mixed condition types are provided, a MixedGroupConditionException will be thrown to enforce consistency and prevent ambiguous behavior.

Special Conditions

BetweenCondition

Purpose: Creates a BETWEEN clause for range queries. Useful for filtering values within a specific range.

Parameters:

Example:

Note

The BetweenCondition requires the value to be an array containing exactly two elements β€” representing the lower and upper bounds of the range for a WHERE BETWEEN comparison.

If the provided array does not contain exactly two elements, an InvalidArgumentException will be thrown to ensure proper condition formatting.

DateCondition

Purpose: Specialized condition for date comparisons. Handles date formatting and comparison.

Parameters:

Example:

InCondition

Purpose: Creates an IN clause for checking if a value exists in a set of values.

Parameters:

Example:

NullCondition

Purpose: Checks if a field is NULL or NOT NULL. Useful for filtering records based on the presence/absence of data.

Parameters:

Example:

RawCondition

Purpose: Allows direct SQL conditions when complex queries are needed. Use with caution and proper parameter binding.

Parameters:

Example:

πŸ”’ Important: Use parameter binding (?) with RawCondition to prevent SQL injection.

WhenCondition

Purpose: Conditionally applies another condition based on a boolean verification. Useful for dynamic query building.

Parameters:

Example:

Best Practices

  1. Type Safety: Always use proper type hints and validate input values before creating conditions.
  2. JSON Operations: Ensure your database supports JSON operations before using JSON-related conditions.
  3. Performance: Consider the impact of complex conditions and nested groups on query performance.
  4. Condition Organization:
    • Group related conditions together
    • Use GroupConditions for complex logical combinations
    • Consider the order of conditions for optimal query performance

Joins

The Laravel Sieve package provides two types of joins to help you build complex queries with proper table relationships.

Join

Purpose: Creates a standard SQL JOIN clause with conditions. This is the most commonly used join type, allowing you to specify the join conditions and add additional conditions to the join clause.

Parameters:

Available Methods

appendCondition(BaseCondition $condition): Join

apply(Builder $builder): void

Example:

ClosureJoin

Purpose: Creates a join using a closure, allowing for more complex join conditions and logic.

Useful when you need to build dynamic or complex join conditions that can't be expressed with simple column comparisons.

Parameters:

Public Methods:

  1. apply(Builder $builder): void

Example:

Note

An InvalidJoinTypeException will be thrown if an unsupported join type is provided.

Best Practices for Joins

Join Naming and Execution Order

When working with joins, it's important to assign clear and descriptive namesβ€”especially when joining the same table multiple times or referencing joins later in your logic.

Named Joins: If a join with the same name already exists, it will be overwritten.

Execution Order: You can control the order in which joins are applied using the appendJoin(BaseJoin $join, int $ order = 100) method. Joins with lower order values are executed first.

Using named and ordered joins helps maintain predictable and maintainable query structures, particularly in complex filtering scenarios.


Sorts

The Laravel Sieve package provides two types of sorts to help you order your query results.

Sort

Purpose: Creates a standard ORDER BY clause for sorting query results. This is the most commonly used sort type, allowing you to sort by a specific column in ascending or descending order.

Parameters:

Available Methods:

apply(Builder $builder): void

Example:

RawSort

Purpose: Creates a raw ORDER BY clause for complex sorting requirements. Useful when you need to use SQL expressions, functions, or complex sorting logic that can't be achieved with simple column sorting.

Parameters:

Available Methods:

apply(Builder $builder): void

Example:

πŸ”’Important: Always use parameter binding with RawSort

Best Practices for Sorts

Raw Sorts Usage:


πŸ“¬ Contact and Feedback

We welcome your feedback and contributions!


All versions of laravel-sieve with dependencies

PHP Build Version
Package Version
Requires php Version >=8.2
illuminate/database Version ^11.0 || ^12.0
illuminate/http Version ^11.0 || ^12.0
illuminate/support Version ^11.0 || ^12.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 architools/laravel-sieve contains the following files

Loading the files please wait ....