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

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

bagtim/laravel-eloquent-case
============================

Adds CASE statement support to Laravel Query Builder.

0441PHP

Since May 14Pushed 2y agoCompare

[ Source](https://github.com/bagtim/laravel-eloquent-case)[ Packagist](https://packagist.org/packages/bagtim/laravel-eloquent-case)[ RSS](/packages/bagtim-laravel-eloquent-case/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependenciesVersions (1)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 &amp; 9.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

17

—

LowBetter than 6% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity12

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity22

Early-stage or recently created project

 Bus Factor1

Top contributor holds 80.4% 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.

### Community

Maintainers

![](https://www.gravatar.com/avatar/6b050706cd4511e10151af881caf51ec8e4470b074a2ccb8f0487b9afe0e5615?d=identicon)[tibogom](/maintainers/tibogom)

---

Top Contributors

[![aglipanci](https://avatars.githubusercontent.com/u/2675283?v=4)](https://github.com/aglipanci "aglipanci (45 commits)")[![eduardlleshi](https://avatars.githubusercontent.com/u/6278258?v=4)](https://github.com/eduardlleshi "eduardlleshi (9 commits)")[![bagtim](https://avatars.githubusercontent.com/u/9790826?v=4)](https://github.com/bagtim "bagtim (2 commits)")

### Embed Badge

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

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

###  Alternatives

[doctrine/orm

Object-Relational-Mapper for PHP

10.2k285.3M6.2k](/packages/doctrine-orm)[jdorn/sql-formatter

a PHP SQL highlighting library

3.9k115.1M102](/packages/jdorn-sql-formatter)[illuminate/database

The Illuminate Database package.

2.8k52.4M9.4k](/packages/illuminate-database)[mongodb/mongodb

MongoDB driver library

1.6k64.0M546](/packages/mongodb-mongodb)[ramsey/uuid-doctrine

Use ramsey/uuid as a Doctrine field type.

90340.3M211](/packages/ramsey-uuid-doctrine)[reliese/laravel

Reliese Components for Laravel Framework code generation.

1.7k3.4M16](/packages/reliese-laravel)

PHPackages © 2026

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