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.3.0(1y ago)13816.6k↑25%5[1 PRs](https://github.com/SiavashBamshadnia/Laravel-Query-Enrich/pulls)MITPHPCI passing

Since Jan 25Pushed 6mo 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 1mo ago

READMEChangelog (7)Dependencies (5)Versions (9)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

44

—

FairBetter than 92% of packages

Maintenance58

Moderate activity, may be stable

Popularity43

Moderate usage in the ecosystem

Community14

Small or concentrated contributor base

Maturity47

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 73.3% 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 ~68 days

Recently: every ~100 days

Total

7

Last Release

437d 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 (44 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)")

---

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

[watson/validating

Eloquent model validating trait.

9723.3M47](/packages/watson-validating)[dyrynda/laravel-model-uuid

This package allows you to easily work with UUIDs in your Laravel models.

4802.8M8](/packages/dyrynda-laravel-model-uuid)[clickbar/laravel-magellan

This package provides functionality for working with the postgis extension in Laravel.

423715.4k1](/packages/clickbar-laravel-magellan)[jerome/filterable

Streamline dynamic Eloquent query filtering with seamless API request integration and advanced caching strategies.

19226.1k](/packages/jerome-filterable)[reedware/laravel-relation-joins

Adds the ability to join on a relationship by name.

2121.2M13](/packages/reedware-laravel-relation-joins)[dragon-code/migrate-db

Easy data transfer from one database to another

15717.4k](/packages/dragon-code-migrate-db)

PHPackages © 2026

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