PHPackages                             sbamtr/laravel-query-enrich - 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. sbamtr/laravel-query-enrich

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

sbamtr/laravel-query-enrich
===========================

A helper for Laravel eloquent and query builder

1.4.0(4w ago)13920.3k↓54%5[1 issues](https://github.com/SiavashBamshadnia/Laravel-Query-Enrich/issues)MITPHPCI passing

Since Jan 25Pushed 4w ago1 watchersCompare

[ Source](https://github.com/SiavashBamshadnia/Laravel-Query-Enrich)[ Packagist](https://packagist.org/packages/sbamtr/laravel-query-enrich)[ RSS](/packages/sbamtr-laravel-query-enrich/feed)WikiDiscussions main Synced 2d ago

READMEChangelog (8)Dependencies (10)Versions (10)Used By (0)

Laravel Query Enrich
====================

[](#laravel-query-enrich)

[![Latest Stable Version](https://camo.githubusercontent.com/8f23eca6bd18bdb5a18e8974fe52c1a14a8e9633abfa1449d65338d9fcfeb3be/687474703a2f2f706f7365722e707567782e6f72672f7362616d74722f6c61726176656c2d71756572792d656e726963682f76)](https://packagist.org/packages/sbamtr/laravel-query-enrich)[![unittests](https://github.com/SiavashBamshadnia/Laravel-Query-Enrich/actions/workflows/unittests.yml/badge.svg)](https://github.com/SiavashBamshadnia/Laravel-Query-Enrich/actions/workflows/unittests.yml)[![CodeFactor](https://camo.githubusercontent.com/15a9677219a8908a286b827628470c9e4a5293294d322cff4d237bff40a941ff/68747470733a2f2f7777772e636f6465666163746f722e696f2f7265706f7369746f72792f6769746875622f7369617661736862616d736861646e69612f6c61726176656c2d71756572792d656e726963682f6261646765)](https://www.codefactor.io/repository/github/siavashbamshadnia/laravel-query-enrich)[![License](https://camo.githubusercontent.com/1d65df328fa6d50a9b2f750202aaf60dc47b0d117e2396518f0b062c40c97dc0/687474703a2f2f706f7365722e707567782e6f72672f7362616d74722f6c61726176656c2d71756572792d656e726963682f6c6963656e7365)](https://github.com/SiavashBamshadnia/Laravel-Query-Enrich/blob/main/LICENSE)

*Supports Laravel 8, 9, 10, 11, 12*

Introduction
------------

[](#introduction)

Laravel Query Enrich makes it easy to create complex database queries in Laravel without having to write complicated SQL code. It simplifies the way developers interact with databases, making it more straightforward to build and read queries in Laravel applications. With Query Enrich, you can achieve advanced database operations without the need for extensive SQL knowledge.

Benefits of Using Laravel Query Enrich:
---------------------------------------

[](#benefits-of-using-laravel-query-enrich)

### No Hard Query Code

[](#no-hard-query-code)

You don't have to struggle with complicated SQL. Laravel Query Enrich makes your queries simpler.

### Ensures Cross-Database Compatibility

[](#ensures-cross-database-compatibility)

If you decide to switch the database engine, Laravel Query Enrich eliminates the need for manual code refactoring. It handles the difference between different database engines and makes things switch smoothly, all without needing programmers to do anything!

### Easy-to-Read Code

[](#easy-to-read-code)

Your code becomes cleaner and easier to understand. Laravel Query Enrich makes interacting with databases in your Laravel applications a breeze.

Example Usage
-------------

[](#example-usage)

Look at the following examples. They are way cooler with the Laravel Query Enrich. Aren't they? :)

### Categorize books into different price categories (using case when)

[](#categorize-books-into-different-price-categories-using-case-when)

#### With Laravel Query Enrich

[](#with-laravel-query-enrich)

```
$books = Book::select(
    'id',
    'name',
    QE::case()
        ->when(c('price'), '>', 100)->then('expensive')
        ->when(QE::condition(50, ' 100 THEN "expensive"
            WHEN price BETWEEN 50 AND 100 THEN "moderate"
            ELSE "affordable"
        END AS price_category
    ')
)->get();
```

#### Raw SQL

[](#raw-sql)

```
select case
           when `price` > 100 then 'expensive'
           when (50 where(c('created_at'), '>=', QE::subDate(QE::now(), 7, Unit::DAY))
    ->get();
```

#### Without Laravel Query Enrich

[](#without-laravel-query-enrich-1)

```
$recentOrders = DB::table('orders')
    ->whereRaw('created_at >= NOW() - INTERVAL ? DAY', 7)
    ->get();
```

#### Raw Query

[](#raw-query)

```
SELECT *
FROM `orders`
WHERE `created_at` >= NOW() - INTERVAL 7 DAY;
```

### Get average monthly price for oil and gas (using avg function)

[](#get-average-monthly-price-for-oil-and-gas-using-avg-function)

#### With Laravel Query Enrich

[](#with-laravel-query-enrich-2)

```
$monthlyPrices = DB::table('prices')
    ->select(
        QE::avg(c('oil'))->as('oil'),
        QE::avg(c('gas'))->as('gas'),
        'month'
    )
    ->groupBy('month')
    ->get();
```

#### Without Laravel Query Enrich

[](#without-laravel-query-enrich-2)

```
$monthlyPrices = DB::table('prices')
    ->select(DB::raw('avg(`oil`) as `oil`, avg(`gas`) as `gas`, `month`'))
    ->groupBy('month')
    ->get();
```

#### Raw Query

[](#raw-query-1)

```
select avg(`oil`) as `oil`, avg(`gas`) as `gas`, `month`
from `prices`
group by `month`
```

### Fetch authors and check if they have any books (using exists query)

[](#fetch-authors-and-check-if-they-have-any-books-using-exists-query)

#### With Laravel Query Enrich

[](#with-laravel-query-enrich-3)

```
$authors = DB::table('authors')->select(
    'id',
    'first_name',
    'last_name',
    QE::exists(
        Db::table('books')->where('books.author_id', c('authors.id'))
    )->as('has_book')
)->orderBy(
    'authors.id'
)->get();
```

#### Without Laravel Query Enrich

[](#without-laravel-query-enrich-3)

```
$authors = DB::table('authors')
->select(
    'id',
    'first_name',
    'last_name',
    DB::raw('exists(select * from `books` where `books`.`author_id` = `authors`.`id`) as `has_book`'))
->orderBy(
    'authors.id',
)
->get();
```

#### Raw Query

[](#raw-query-2)

```
select `id`,
       `first_name`,
       `last_name`,
       exists(select * from `books` where `books`.`author_id` = `authors`.`id`) as `result`
from `authors`
order by `authors`.`id` asc
```

### Getting full name on database level (using concatws)

[](#getting-full-name-on-database-level-using-concatws)

#### With Laravel Query Enrich

[](#with-laravel-query-enrich-4)

```
$authors = Author::select(
    'first_name',
    'last_name',
    QE::concatWS(' ', c('first_name'), c('last_name'))->as('result')
)->get();
```

#### Without Laravel Query Enrich

[](#without-laravel-query-enrich-4)

```
$author = Author::select(
    'first_name',
    'last_name',
    DB::raw("concat_ws(' ', `first_name`, `last_name`) as `result`")
)->first();
```

#### Raw Query

[](#raw-query-3)

```
select `first_name`, `last_name`, concat_ws(' ', `first_name`, `last_name`) as `result`
from `authors`
```

Installation and documentation
------------------------------

[](#installation-and-documentation)

The complete documentation is available here:

License
-------

[](#license)

The MIT License (MIT). Please see the [License file](LICENSE) for more information.

---

Written with ♥ by Siavash Bamshadnia.

Please support me by staring this repository.

###  Health Score

53

—

FairBetter than 96% of packages

Maintenance94

Actively maintained with recent releases

Popularity43

Moderate usage in the ecosystem

Community15

Small or concentrated contributor base

Maturity48

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 73% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~123 days

Recently: every ~214 days

Total

8

Last Release

29d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/e03c63cfce30aa1647ab27500569c64448187c5b84de422d32a4805f464c1614?d=identicon)[sbamtr](/maintainers/sbamtr)

---

Top Contributors

[![SiavashBamshadnia](https://avatars.githubusercontent.com/u/3998368?v=4)](https://github.com/SiavashBamshadnia "SiavashBamshadnia (46 commits)")[![hosni](https://avatars.githubusercontent.com/u/47793698?v=4)](https://github.com/hosni "hosni (9 commits)")[![StyleCIBot](https://avatars.githubusercontent.com/u/11048387?v=4)](https://github.com/StyleCIBot "StyleCIBot (5 commits)")[![yeganemehr](https://avatars.githubusercontent.com/u/16887332?v=4)](https://github.com/yeganemehr "yeganemehr (2 commits)")[![laravel-shift](https://avatars.githubusercontent.com/u/15991828?v=4)](https://github.com/laravel-shift "laravel-shift (1 commits)")

---

Tags

eloquentlaravelphpquerybuilder

### Embed Badge

![Health badge](/badges/sbamtr-laravel-query-enrich/health.svg)

```
[![Health](https://phpackages.com/badges/sbamtr-laravel-query-enrich/health.svg)](https://phpackages.com/packages/sbamtr-laravel-query-enrich)
```

###  Alternatives

[psalm/plugin-laravel

Psalm plugin for Laravel

3355.3M346](/packages/psalm-plugin-laravel)[laravel/horizon

Dashboard and code-driven configuration for Laravel queues.

4.2k95.4M306](/packages/laravel-horizon)[laravel/cashier

Laravel Cashier provides an expressive, fluent interface to Stripe's subscription billing services.

2.6k29.9M146](/packages/laravel-cashier)[laravel/pulse

Laravel Pulse is a real-time application performance monitoring tool and dashboard for your Laravel application.

1.7k15.1M132](/packages/laravel-pulse)[laravel/ai

The official AI SDK for Laravel.

1.0k3.2M194](/packages/laravel-ai)[api-platform/laravel

API Platform support for Laravel

58171.5k14](/packages/api-platform-laravel)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
