PHPackages                             aglipanci/laravel-eloquent-case - 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. aglipanci/laravel-eloquent-case

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

aglipanci/laravel-eloquent-case
===============================

Adds CASE statement support to Laravel Query Builder.

5.1.0(5mo ago)116237.1k↑10.7%10MITPHPPHP ^8.2CI failing

Since Feb 27Pushed 1mo ago2 watchersCompare

[ Source](https://github.com/aglipanci/laravel-eloquent-case)[ Packagist](https://packagist.org/packages/aglipanci/laravel-eloquent-case)[ Docs](https://github.com/aglipanci/laravel-case)[ RSS](/packages/aglipanci-laravel-eloquent-case/feed)WikiDiscussions main Synced 2w ago

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

Laravel Eloquent CASE Statement Support
=======================================

[](#laravel-eloquent-case-statement-support)

[![Test Status](https://camo.githubusercontent.com/cec2b853193401d8c075e4ca3e62c0fe8e55791f3389d5fe5d4a7c7bfdb18403/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f61676c6970616e63692f6c61726176656c2d656c6f7175656e742d636173652f72756e2d74657374732e796d6c3f6272616e63683d6d61696e)](https://camo.githubusercontent.com/cec2b853193401d8c075e4ca3e62c0fe8e55791f3389d5fe5d4a7c7bfdb18403/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f61676c6970616e63692f6c61726176656c2d656c6f7175656e742d636173652f72756e2d74657374732e796d6c3f6272616e63683d6d61696e)

This packages adds [CASE](https://dev.mysql.com/doc/refman/5.7/en/flow-control-functions.html#operator_case) statement support to Laravel Query Builder. It supports Laravel 8.x, 9.x, 10.x, 11.x, 12.x &amp; 13.x.

Usage
-----

[](#usage)

### Add a CASE statement select on a Laravel Query

[](#add-a-case-statement-select-on-a-laravel-query)

```
use App\Models\Invoice;
use AgliPanci\LaravelCase\Query\CaseBuilder;

$invoices = Invoice::query()
            ->case(function (CaseBuilder $case) {
                $case->when('balance', '', 0)->then('Balance Due');

$invoices = Invoice::query()
            ->case($caseQuery, 'payment_status')
            ->get();
```

### Raw CASE conditions

[](#raw-case-conditions)

```
use App\Models\Invoice;
use AgliPanci\LaravelCase\Facades\CaseBuilder;

$caseQuery = CaseBuilder::whenRaw('balance = ?', [0])->thenRaw("'Paid'")
                    ->elseRaw("'N/A'")

$invoices = Invoice::query()
            ->case($caseQuery, 'payment_status')
            ->get();
```

### Use as raw SELECT

[](#use-as-raw-select)

```
use App\Models\Invoice;
use \AgliPanci\LaravelCase\Facades\CaseBuilder;

$caseQuery = CaseBuilder::whenRaw('balance = ?', [0])->thenRaw("'Paid'")
                    ->elseRaw("'N/A'")

$invoices = Invoice::query()
            ->selectRaw($caseQuery->toRaw())
            ->get();
```

### Available methods

[](#available-methods)

```
use AgliPanci\LaravelCase\Facades\CaseBuilder;

$caseQuery = CaseBuilder::whenRaw('balance = ?', [0])->thenRaw("'Paid'")
                    ->elseRaw("'N/A'");

// Get the SQL representation of the query.
$caseQuery->toSql();

// Get the query bindings.
$caseQuery->getBindings();

// Get the SQL representation of the query with bindings.
$caseQuery->toRaw();

 // Get an Illuminate\Database\Query\Builder instance.
$caseQuery->toQuery();
```

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

[](#installation)

You can install the package via composer:

```
composer require aglipanci/laravel-eloquent-case
```

### Testing

[](#testing)

```
composer test
```

### Changelog

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.

### Security

[](#security)

If you discover any security related issues, please email  instead of using the issue tracker.

Credits
-------

[](#credits)

- [Agli Pançi](https://github.com/aglipanci)
- [Eduard Lleshi](https://github.com/eduardlleshi)
- [All Contributors](https://github.com/aglipanci/laravel-case/graphs/contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

60

—

FairBetter than 98% of packages

Maintenance84

Actively maintained with recent releases

Popularity51

Moderate usage in the ecosystem

Community16

Small or concentrated contributor base

Maturity70

Established project with proven stability

 Bus Factor1

Top contributor holds 84.7% 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 ~134 days

Recently: every ~183 days

Total

12

Last Release

154d ago

Major Versions

0.0.1 → 1.0.02022-02-27

v1.4.0 → 2.02023-02-15

2.0 → 3.0.02024-03-12

3.1.0 → 4.0.02025-02-18

4.0.0 → 5.0.02026-03-17

PHP version history (3 changes)0.0.1PHP ^7.4|^8.0

4.0.0PHP ^8.0

5.0.0PHP ^8.2

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/2675283?v=4)[Agli Pançi](/maintainers/aglipanci)[@aglipanci](https://github.com/aglipanci)

---

Top Contributors

[![aglipanci](https://avatars.githubusercontent.com/u/2675283?v=4)](https://github.com/aglipanci "aglipanci (72 commits)")[![eduardlleshi](https://avatars.githubusercontent.com/u/6278258?v=4)](https://github.com/eduardlleshi "eduardlleshi (9 commits)")[![laravel-shift](https://avatars.githubusercontent.com/u/15991828?v=4)](https://github.com/laravel-shift "laravel-shift (2 commits)")[![macbook-n-cheese](https://avatars.githubusercontent.com/u/265233296?v=4)](https://github.com/macbook-n-cheese "macbook-n-cheese (2 commits)")

---

Tags

eloquentlaravelormphp

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

### Embed Badge

![Health badge](/badges/aglipanci-laravel-eloquent-case/health.svg)

```
[![Health](https://phpackages.com/badges/aglipanci-laravel-eloquent-case/health.svg)](https://phpackages.com/packages/aglipanci-laravel-eloquent-case)
```

###  Alternatives

[mongodb/laravel-mongodb

A MongoDB based Eloquent model and Query builder for Laravel

7.1k8.9M110](/packages/mongodb-laravel-mongodb)[psalm/plugin-laravel

Psalm plugin for Laravel

3345.4M354](/packages/psalm-plugin-laravel)[yajra/laravel-oci8

Oracle DB driver for Laravel via OCI8

8723.3M27](/packages/yajra-laravel-oci8)[glushkovds/phpclickhouse-laravel

Adapter of the most popular library https://github.com/smi2/phpClickHouse to Laravel

2051.6M2](/packages/glushkovds-phpclickhouse-laravel)[api-platform/laravel

API Platform support for Laravel

58190.1k21](/packages/api-platform-laravel)[laravel-liberu/laravel-gedcom

A package that converts gedcom files to Eloquent models

792.5k1](/packages/laravel-liberu-laravel-gedcom)

PHPackages © 2026

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