Laravel Eloquent CASE Statement Support

Overview

Laravel Eloquent CASE Statement Support

Test Status

This packages adds CASE statement support to Laravel Query Builder. It supports Laravel 8.x & 9.x.

Usage

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('Overpaid')
                    ->when('balance', 0)->then('Paid')
                    ->else('Balance Due');
            }, 'payment_status')
            ->get();

Produces the following SQL query:

SELECT
  ( CASE
      WHEN `balance` < 0 THEN 'Overpaid'
      WHEN `balance` = 0 THEN 'Paid'
      ELSE 'Balance Due'
    END ) AS `payment_status`
FROM
  `invoices`

Build the case query separately

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

$caseQuery = CaseBuilder::when('balance', 0)->then('Paid')
                    ->when('balance', '>', 0)->then('Balance Due');
                    
$invoices = Invoice::query()
            ->case($caseQuery, 'payment_status')
            ->get();

Raw CASE conditions

elseRaw("'N/A'") $invoices = Invoice::query() ->case($caseQuery, 'payment_status') ->get();">
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

elseRaw("'N/A'") $invoices = Invoice::query() ->selectRaw($caseQuery->toRaw()) ->get();">
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

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();">
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

You can install the package via composer:

composer require aglipanci/laravel-eloquent-case

Testing

composer test

Changelog

Please see CHANGELOG for more information what has changed recently.

Security

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

Credits

License

The MIT License (MIT). Please see License File for more information.

You might also like...
Orbit is a flat-file driver for Laravel Eloquent
Orbit is a flat-file driver for Laravel Eloquent

Orbit is a flat-file driver for Laravel Eloquent. It allows you to replace your generic database with real files that you can manipulate using the methods you're familiar with.

Collection of the Laravel/Eloquent Model classes that allows you to get data directly from a Magento 2 database.

Laragento LAravel MAgento Micro services Magento 2 has legacy code based on abandoned Zend Framework 1 with really ugly ORM on top of outdated Zend_DB

The missing laravel helper that allows you to inspect your eloquent queries with it's bind parameters

Laravel Query Inspector The missing laravel helper that allows you to ispect your eloquent queries with it's bind parameters Motivations Let's say you

Laravel Quran is static Eloquent model for Quran.

Laravel Quran بِسْمِ ٱللّٰهِ الرَّحْمٰنِ الرَّحِيْمِ Laravel Quran is static Eloquent model for Quran. The Quran has never changed and never will, bec

A package for Laravel One Time Password (OTP) generator and validation without Eloquent Model, since it done by Cache.

Laravel OTP Introduction A package for Laravel One Time Password (OTP) generator and validation without Eloquent Model, since it done by Cache. The ca

 Provides a Eloquent query builder for Laravel or Lumen
Provides a Eloquent query builder for Laravel or Lumen

This package provides an advanced filter for Laravel or Lumen model based on incoming requets.

Curso Laravel Eloquent ORM

Curso Laravel Eloquent ORM

Laravel basic Functions, eloquent cruds, query filters, constants

Emmanuelpcg laravel-basics Description Package with basic starter features for Laravel. Install If Builder Constants Install composer require emmanuel

An extended laravel eloquent WHERE method to work with sql LIKE operator.

Laravel Eloquent WhereLike An extended laravel eloquent WHERE method to work with sql LIKE operator. Inspiration The idea of this package comes from o

Comments
  • add support caseRaw

    add support caseRaw

    CaseRaw support required

    To be able to do things like this

    case count(*)

    an example of how it would look in code

    CaseBuilder::caseRaw('count(id)')
        ->whenRaw(0)->then(0)
        ->else(100)
        ->toRaw();
    

    output

    case count(id) when 0 then 0 else 100 end
    
    opened by entense 1
Releases(v1.4.0)
Owner
Agli Pançi
Web Developer
Agli Pançi
Tag support for Laravel Eloquent models - Taggable Trait

Laravel Taggable Trait This package is not meant to handle javascript or html in any way. This package handles database storage and read/writes only.

Rob 859 Dec 11, 2022
Package with small support traits and classes for the Laravel Eloquent models

Contains a set of traits for the eloquent model. In future can contain more set of classes/traits for the eloquent database.

Martin Kluska 3 Feb 10, 2022
A simple drop-in solution for providing UUID support for the IDs of your Eloquent models.

Introduction A simple drop-in solution for providing UUID support for the IDs of your Eloquent models. Both v1 and v4 IDs are supported out of the box

GoldSpec Digital 501 Jan 4, 2023
Laravel comments - This package enables to easily associate comments to any Eloquent model in your Laravel application

Laravel comments - This package enables to easily associate comments to any Eloquent model in your Laravel application

Rubik 4 May 12, 2022
This Laravel package merges staudenmeir/eloquent-param-limit-fix and staudenmeir/laravel-adjacency-list to allow them being used in the same model.

This Laravel package merges staudenmeir/eloquent-param-limit-fix and staudenmeir/laravel-adjacency-list to allow them being used in the same model.

Jonas Staudenmeir 5 Jan 6, 2023
An Eloquent Way To Filter Laravel Models And Their Relationships

Eloquent Filter An Eloquent way to filter Eloquent Models and their relationships Introduction Lets say we want to return a list of users filtered by

Eric Tucker 1.5k Jan 7, 2023
Easy creation of slugs for your Eloquent models in Laravel

Eloquent-Sluggable Easy creation of slugs for your Eloquent models in Laravel. NOTE: These instructions are for the latest version of Laravel. If you

Colin Viebrock 3.6k Dec 30, 2022
Automatically validating Eloquent models for Laravel

Validating, a validation trait for Laravel Validating is a trait for Laravel Eloquent models which ensures that models meet their validation criteria

Dwight Watson 955 Dec 25, 2022
Laravel Ban simplify blocking and banning Eloquent models.

Laravel Ban Introduction Laravel Ban simplify management of Eloquent model's ban. Make any model bannable in a minutes! Use case is not limited to Use

cybercog 879 Dec 30, 2022
cybercog 996 Dec 28, 2022