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

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

entense/laravel-eloquent-case
=============================

Adds CASE statement support to Laravel Query Builder.

1.0.0(4y ago)06MITPHPPHP ^7.4|^8.0

Since Apr 7Pushed 3y agoCompare

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

READMEChangelogDependencies (6)Versions (2)Used By (0)

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

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

[![Test Status](https://camo.githubusercontent.com/8faf69b159354afe54fbd0a585522ada4c217a34b9838803bc8ab2fafdc48e27/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f776f726b666c6f772f7374617475732f61676c6970616e63692f6c61726176656c2d656c6f7175656e742d636173652f72756e2d74657374733f6c6162656c3d7465737473)](https://camo.githubusercontent.com/8faf69b159354afe54fbd0a585522ada4c217a34b9838803bc8ab2fafdc48e27/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f776f726b666c6f772f7374617475732f61676c6970616e63692f6c61726176656c2d656c6f7175656e742d636173652f72756e2d74657374733f6c6162656c3d7465737473)

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

23

—

LowBetter than 27% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity4

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity53

Maturing project, gaining track record

 Bus Factor1

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

Unknown

Total

1

Last Release

1497d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/9099b234bd4713374af10ca69068d87ba22461d12f38f226335e7eacd302c6c3?d=identicon)[entense](/maintainers/entense)

---

Top Contributors

[![aglipanci](https://avatars.githubusercontent.com/u/2675283?v=4)](https://github.com/aglipanci "aglipanci (43 commits)")[![eduardlleshi](https://avatars.githubusercontent.com/u/6278258?v=4)](https://github.com/eduardlleshi "eduardlleshi (7 commits)")[![entense](https://avatars.githubusercontent.com/u/76127644?v=4)](https://github.com/entense "entense (1 commits)")

###  Code Quality

TestsPHPUnit

Code StylePHP CS Fixer

### Embed Badge

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

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

###  Alternatives

[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)[aglipanci/laravel-eloquent-case

Adds CASE statement support to Laravel Query Builder.

115157.2k](/packages/aglipanci-laravel-eloquent-case)

PHPackages © 2026

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