Download the PHP package bytegourmet/laravel-conditional-expressions without Composer
On this page you can find all versions of the php package bytegourmet/laravel-conditional-expressions. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download bytegourmet/laravel-conditional-expressions
More information about bytegourmet/laravel-conditional-expressions
Files in bytegourmet/laravel-conditional-expressions
Package laravel-conditional-expressions
Short Description Fluent CASE WHEN expression builder for Laravel
License MIT
Informations about the package laravel-conditional-expressions
Laravel Conditional Expressions
A fluent CASE WHEN expression builder for Laravel that makes it easy to write complex conditional SQL expressions in your queries.
Features
- 🎯 Fluent API - Chain methods for readable CASE expressions
- 🔄 Nested CASE - Support for nested CASE expressions
- 🛠️ Complex Conditions - Use
CaseBuilderfor complex WHERE conditions - 📦 Laravel Integration - Works seamlessly with Eloquent and Query Builder
- 🔒 Parameter Binding - Automatic parameter binding for security
- 🎨 Simple & Searched CASE - Support for both CASE types
Requirements
- PHP >= 8.2
- Laravel >= 10.0
Installation
Standard Installation
Install the package via Composer:
The package will automatically register its service provider.
Quick Start
Basic Usage
Usage
Simple CASE Expression
A simple CASE expression compares a column against multiple values:
Searched CASE Expression
A searched CASE expression evaluates conditions (no column comparison):
Using in Queries
With Eloquent Models
With Query Builder
Nested CASE Expressions
You can nest CASE expressions for complex logic:
Complex Conditions with CaseBuilder
For complex WHERE conditions in your CASE expressions, use CaseBuilder:
Advanced CaseBuilder Usage
Using the selectCase Macro
The package provides a convenient selectCase macro for cleaner syntax:
Multiple CASE Expressions
You can use multiple CASE expressions in a single query:
Real-World Examples
Example 1: User Status with Multiple Conditions
Example 2: Order Priority Based on Amount and Date
Example 3: Dynamic Pricing Tiers
API Reference
CaseExpr
The main facade class for creating CASE expressions.
Methods
CaseExpr::make(?string $column = null)
Creates a new searched CASE expression (or simple CASE if column is provided).
CaseExpr::simple(string $column)
Creates a simple CASE expression for the given column.
CaseExpr::builder()
Creates a new CaseBuilder instance for complex conditions.
CaseExpression
The expression class that builds the CASE statement.
Methods
when(string|CaseBuilder|Expression $column, ?string $operator = null, mixed $value = null)
Adds a WHEN clause to the CASE expression.
then(mixed $result)
Sets the THEN value for the last WHEN clause. Accepts scalar values or nested CaseExpression.
else(mixed $result)
Sets the ELSE value. Accepts scalar values or nested CaseExpression.
as(string $alias)
Sets an alias for the CASE expression.
toSql()
Returns the generated SQL string.
getBindings()
Returns the parameter bindings array.
CaseBuilder
Extends Laravel's Query Builder for building complex WHERE conditions.
Methods
CaseBuilder::query()
Creates a new CaseBuilder instance.
getConditions()
Returns the compiled WHERE conditions as a string.
All standard Laravel Query Builder methods are available (where, orWhere, whereNull, etc.).
Debugging
You can debug your CASE expressions using the provided helper methods:
Testing
Run the test suite:
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
This package is open-sourced software licensed under the MIT license.
Credits
- Author: Riju Ghosh
All versions of laravel-conditional-expressions with dependencies
illuminate/database Version ^10.0|^11.0|^12.0
illuminate/support Version ^10.0|^11.0|^12.0