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(2mo ago)115157.2k↑20.1%10MITPHPPHP ^8.2CI failing

Since Feb 27Pushed 2mo 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 1mo ago

READMEChangelog (10)Dependencies (10)Versions (14)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 99% of packages

Maintenance87

Actively maintained with recent releases

Popularity49

Moderate usage in the ecosystem

Community16

Small or concentrated contributor base

Maturity69

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

62d 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://www.gravatar.com/avatar/6ab3935dbfd17d02014b655a60004bf9f43a9367c3365d36c2308297eb01b158?d=identicon)[aglipanci](/maintainers/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

[watson/validating

Eloquent model validating trait.

9723.3M47](/packages/watson-validating)[cybercog/laravel-love

Make Laravel Eloquent models reactable with any type of emotions in a minutes!

1.2k302.7k1](/packages/cybercog-laravel-love)[cviebrock/eloquent-taggable

Easy ability to tag your Eloquent models in Laravel.

567694.8k3](/packages/cviebrock-eloquent-taggable)[clickbar/laravel-magellan

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

423715.4k1](/packages/clickbar-laravel-magellan)[genealabs/laravel-pivot-events

This package introduces new eloquent events for sync(), attach(), detach() or updateExistingPivot() methods on BelongsToMany relation.

1404.9M8](/packages/genealabs-laravel-pivot-events)[reedware/laravel-relation-joins

Adds the ability to join on a relationship by name.

2121.2M13](/packages/reedware-laravel-relation-joins)

PHPackages © 2026

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