PHPackages                             eltabarani/reusable-query - 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. eltabarani/reusable-query

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

eltabarani/reusable-query
=========================

Reusable and modular Eloquent query filters for Laravel

v1.1.0(9mo ago)5111MITPHPPHP ^8.0CI passing

Since Jul 23Pushed 7mo agoCompare

[ Source](https://github.com/AhmedElTabarani/reusable-query)[ Packagist](https://packagist.org/packages/eltabarani/reusable-query)[ Docs](https://github.com/eltabarani/reusable-query)[ RSS](/packages/eltabarani-reusable-query/feed)WikiDiscussions main Synced 1mo ago

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

Laravel Reusable Query
======================

[](#laravel-reusable-query)

[![Latest Version on Packagist](https://camo.githubusercontent.com/d42010e2172694bb3adf47b5372f55374fc2ca038f13a0d2ba66e4d72f3d1def/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f656c7461626172616e692f7265757361626c652d71756572792e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/eltabarani/reusable-query)[![Total Downloads](https://camo.githubusercontent.com/040588d751d24915623d888a75a9b31a207410db295874a69c01df5434d3f2f2/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f656c7461626172616e692f7265757361626c652d71756572792e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/eltabarani/reusable-query)

This package provides a elegant solution for creating and managing reusable query filters within Laravel applications.

---

When you deal with Global Scopes in Laravel, you often need to add a `identifier` then the scope object like this:

```
$activeUsers = User::withGlobalScope('is_active', new IsActiveScope())->get();
```

This syntax can become overwhelming, especially when you have multiple scopes or complex query logic.

So i decided to my own simple and elegant class, then i decided to share it with the community. Not a big deal, but it works fine for me and i hope it will help you too.

So here is the new syntax:

```
$activeUsers = User::useQuery(IsActiveQuery::class)->get();
```

I believe this syntax is much cleaner and easier to read.

Overview
--------

[](#overview)

The Laravel Reusable Query package simplifies the application of reusable query filters across Eloquent models. It compatible with Scopes, Closures, and our own elegant syntax for defining reusable queries. This package is designed to simplify query management, enhance code readability, and be compatible with all.

Key Features
------------

[](#key-features)

- **Simple Syntax**: Apply queries with clear, concise syntax.
- **Model Compatibility**: Works with all Eloquent models.
- **Flexible Parameters**: Supports class names, closures, and Eloquent scopes.
- **Reusable Logic**: Define query logic once, use across multiple models.

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

[](#installation)

To integrate the Reusable Query package into your Laravel project, execute the following command via Composer:

```
composer require eltabarani/reusable-query
```

Quick Start Guide
-----------------

[](#quick-start-guide)

Follow these steps to quickly implement and utilize Reusable Query in your application:

### 1. Create a Query Class

[](#1-create-a-query-class)

Define your reusable query logic by creating a class that implements the `ReusableQueryContract` interface. For example, to filter active records:

```
