PHP code example of aglipanci / laravel-eloquent-case

1. Go to this page and download the library: Download aglipanci/laravel-eloquent-case library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

aglipanci / laravel-eloquent-case example snippets


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

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

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

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