Download the PHP package tpetry/laravel-query-expressions without Composer
On this page you can find all versions of the php package tpetry/laravel-query-expressions. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download tpetry/laravel-query-expressions
More information about tpetry/laravel-query-expressions
Files in tpetry/laravel-query-expressions
Package laravel-query-expressions
Short Description Database-independent Query Expressions as a replacement to DB::raw calls
License MIT
Homepage https://github.com/tpetry/laravel-query-expressions
Informations about the package laravel-query-expressions
Laravel Query Expressions to replace DB::raw() calls
Laravel's database implementation provides a good way of working with multiple databases while abstracting away their inner workings. You don't have to consider minor syntax differences when using a query builder or how each database handles specific operations slightly differently.
However, when we want to use more database functionality than Laravel provides, we must fall back to raw SQL expressions and write database-specific code. The Query Expressions package builds on new features introduced in Laravel 10 to solve that problem. All provided implementations abstract some SQL functionality that is automatically transformed to the correct syntax with the same behaviour for your used database engine. And if your version is still supported by Laravel but is missing a feature, it is emulated by the implementations. So you can even do things that were not possible before.
You can make your queries database independent:
And you can also create new powerful queries:
Installation
You can install the package via composer:
Usage
This package implements a lot of expressions you can use for selecting data, do better filtering or ordering of rows.
Every expression can be used exactly as stated by the documentation, but you can also combine them as shared in the example before.
Whenever an expression class needs a string|Expression
parameter, you can pass a column name or another (deeply nested) expression object.
Note A string passed for a
string|Expression
parameter is always used as a column name that will be automatically quoted.Warning The generated SQL statements of the examples are only for explanatory purposes. The real ones will be automatically tailored to your database using proper quoting and its specific syntax.
Language
Values
As stated before, an expression is always a column name.
But if you want to e.g. do an equality check, you may want to compare something to a specific value.
That's where you should use the Value
class.
Its values will always be automatically escaped within the query.
Note The
Value
class in isolation is not that usefull. But it will be used more in the next examples.
Alias
Note The
Alias
class in isolation is not that usefull because Eloquent can already do this. But it will be used more in the next examples.
Cast
Case-When
To use the case-when in a Select
wrap it in an Alias
:
Operators
Arithmetic Operators
Bitwise Operators
Comparison & Logical Operators
You can use these expressions directly with Laravel's where()
method:
Functions
Aggregates
Conditional
Math
String
Warning The
Uuid4
expression is not available for all database versions. With PostgreSQL you need at least v13 and with MariaDB at least v10.10.
Time
Changelog
Please see CHANGELOG for more information on what has changed recently.
Credits
- tpetry
- All Contributors
License
The MIT License (MIT). Please see License File for more information.
All versions of laravel-query-expressions with dependencies
illuminate/contracts Version ^10.13.1|^11.0
illuminate/database Version ^10.13.1|^11.0
illuminate/support Version ^10.0|^11.0