PHP code example of bytegourmet / laravel-conditional-expressions

1. Go to this page and download the library: Download bytegourmet/laravel-conditional-expressions 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/ */

    

bytegourmet / laravel-conditional-expressions example snippets


use ByteGourmet\LaravelConditionalExpressions\CaseExpr;

$users = User::query()
    ->select([
        'id',
        'name',
        CaseExpr::make()
            ->when('status', '=', 'active')
            ->then('Active User')
            ->when('status', '=', 'inactive')
            ->then('Inactive User')
            ->else('Unknown')
            ->as('status_label')
    ])
    ->get();

use ByteGourmet\LaravelConditionalExpressions\CaseExpr;

$case = CaseExpr::simple('status')
    ->when('active')
    ->then('User is active')
    ->when('inactive')
    ->then('User is inactive')
    ->else('Unknown status')
    ->as('status_description');

// Generates: CASE `status` WHEN 'active' THEN 'User is active' WHEN 'inactive' THEN 'User is inactive' ELSE 'Unknown status' END AS `status_description`

use ByteGourmet\LaravelConditionalExpressions\CaseExpr;

$case = CaseExpr::make()
    ->when('age', '>=', 18)
    ->then('Adult')
    ->when('age', '>=', 13)
    ->then('Teenager')
    ->else('Child')
    ->as('age_category');

// Generates: CASE WHEN `age` >= ? THEN ? WHEN `age` >= ? THEN ? ELSE ? END AS `age_category`

use App\Models\User;
use ByteGourmet\LaravelConditionalExpressions\CaseExpr;

$users = User::query()
    ->select([
        'id',
        'name',
        'email',
        CaseExpr::make()
            ->when('email_verified_at', '!=', null)
            ->then('Verified')
            ->else('Unverified')
            ->as('verification_status')
    ])
    ->get();

use Illuminate\Support\Facades\DB;
use ByteGourmet\LaravelConditionalExpressions\CaseExpr;

$results = DB::table('orders')
    ->select([
        'id',
        'total',
        CaseExpr::make()
            ->when('total', '>', 1000)
            ->then('High Value')
            ->when('total', '>', 500)
            ->then('Medium Value')
            ->else('Low Value')
            ->as('order_category')
    ])
    ->get();

use ByteGourmet\LaravelConditionalExpressions\CaseExpr;

$case = CaseExpr::make()
    ->when('phone_verified', '=', true)
    ->then(
        CaseExpr::make()
            ->when('email_verified', '=', true)
            ->then('Fully Verified')
            ->else('Phone Verified Only')
    )
    ->else('Unverified')
    ->as('verification_status');

use ByteGourmet\LaravelConditionalExpressions\CaseExpr;

$caseBuilder = CaseExpr::builder()
    ->where('status', 'active')
    ->where('email_verified_at', '!=', null)
    ->orWhere('phone_verified_at', '!=', null);

$case = CaseExpr::make()
    ->when($caseBuilder)
    ->then('Fully Verified')
    ->when('status', '=', 'pending')
    ->then('Pending Verification')
    ->else('Unverified')
    ->as('account_status');

use ByteGourmet\LaravelConditionalExpressions\CaseExpr;

$caseBuilder = CaseExpr::builder()
    ->where('a', 1)
    ->where('b', '>', 2)
    ->orWhere([
        'c' => 3,
        'd' => '4',
        ['e', '!=', 5],
    ])
    ->where(function ($query) {
        $query->whereNull('f');
    });

$case = CaseExpr::make()
    ->when($caseBuilder)
    ->then('Complex Condition Met')
    ->else('Condition Not Met')
    ->as('result');

use App\Models\User;

$users = User::query()
    ->select(['id', 'name'])
    ->selectCase('status_label', function ($case) {
        $case
            ->when('status', '=', 'active')
            ->then('Active')
            ->when('status', '=', 'inactive')
            ->then('Inactive')
            ->else('Unknown');
    })
    ->get();

use ByteGourmet\LaravelConditionalExpressions\CaseExpr;

$users = User::query()
    ->select([
        'id',
        'name',
        CaseExpr::make()
            ->when('email_verified_at', '!=', null)
            ->then('Verified')
            ->else('Unverified')
            ->as('email_status'),
        CaseExpr::make()
            ->when('phone_verified_at', '!=', null)
            ->then('Verified')
            ->else('Unverified')
            ->as('phone_status'),
        CaseExpr::make()
            ->when('created_at', '>', now()->subDays(30))
            ->then('New User')
            ->else('Existing User')
            ->as('user_type')
    ])
    ->get();

use App\Models\User;
use ByteGourmet\LaravelConditionalExpressions\CaseExpr;

$users = User::query()
    ->select([
        'id',
        'name',
        CaseExpr::make()
            ->when('email_verified_at', '!=', null)
            ->then(
                CaseExpr::make()
                    ->when('phone_verified_at', '!=', null)
                    ->then('Fully Verified')
                    ->else('Email Verified Only')
            )
            ->when('phone_verified_at', '!=', null)
            ->then('Phone Verified Only')
            ->else('Unverified')
            ->as('verification_status')
    ])
    ->get();

use Illuminate\Support\Facades\DB;
use ByteGourmet\LaravelConditionalExpressions\CaseExpr;

$orders = DB::table('orders')
    ->select([
        'id',
        'customer_id',
        'total',
        CaseExpr::make()
            ->when('total', '>', 1000)
            ->then('High Priority')
            ->when('total', '>', 500)
            ->then(
                CaseExpr::make()
                    ->when('created_at', '>', now()->subDays(7))
                    ->then('Medium Priority - Recent')
                    ->else('Medium Priority')
            )
            ->else('Low Priority')
            ->as('priority')
    ])
    ->get();

use App\Models\Product;
use ByteGourmet\LaravelConditionalExpressions\CaseExpr;

$products = Product::query()
    ->select([
        'id',
        'name',
        'price',
        CaseExpr::make()
            ->when('price', '>=', 1000)
            ->then('Premium')
            ->when('price', '>=', 500)
            ->then('Standard')
            ->when('price', '>=', 100)
            ->then('Basic')
            ->else('Economy')
            ->as('tier')
    ])
    ->get();

$case = CaseExpr::make(); // Searched CASE
$case = CaseExpr::make('status'); // Simple CASE

$case = CaseExpr::simple('status');

$builder = CaseExpr::builder();

$case->when('status', '=', 'active');
$case->when($caseBuilder); // Using CaseBuilder

$case->then('Active');
$case->then(CaseExpr::make()->when(...)->then(...)); // Nested

$case->else('Unknown');

$case->as('status_label');

$sql = $case->toSql();

$bindings = $case->getBindings();

$builder = CaseExpr::builder();

$conditions = $builder->getConditions();

$case = CaseExpr::make()
    ->when('status', '=', 'active')
    ->then('Active')
    ->else('Inactive');

// Dump the SQL
$case->dump();

// Dump and die
$case->dd();