用表达式替换任何查询原始调用
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:
// Instead of:
User::query()
->when(isPostgreSQL(), fn ($query) => $query->selectRaw('coalesce("user", "admin") AS "value"'))
->when(isMySQL(), fn ($query) => $query->selectRaw('coalesce(`user`, `admin`) AS `value`'))
// You can use:
User::select(new Alias(new Coalesce(['user', 'admin']), 'value'));And you can also create new powerful queries:
// Aggregate multiple statistics with one query for dashboards:
Movie::select([
new CountFilter(new Equal('released', new Value(2021))),
new CountFilter(new Equal('released', new Value(2022))),
new CountFilter(new Equal('genre', new Value('Drama'))),
new CountFilter(new Equal('genre', new Value('Comedy'))),
])->where('streamingservice', 'netflix');You can install the package via composer:
composer require tpetry/laravel-query-expressionsThis 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|Expressionparameter 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.
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.
use Tpetry\QueryExpressions\Value\Value;
new Value(42);
new Value("Robert'); DROP TABLE students;--");Note The
Valueclass in isolation is not that usefull. But it will be used more in the next examples.
use Illuminate\Contracts\Database\Query\Expression;
use Tpetry\QueryExpressions\Language\Alias;
use Tpetry\QueryExpressions\Value\Value;
new Alias(string|Expression $expression, string $name)
User::select([
new Alias('last_modified_at', 'modification_date'),
new Alias(new Value(21), 'min_age_threshold'),
])->get();Note The
Aliasclass in isolation is not that usefull because Eloquent can already do this. But it will be used more in the next examples.
use Illuminate\Contracts\Database\Query\Expression;
use Tpetry\QueryExpressions\Language\Alias;
use Tpetry\QueryExpressions\Language\Cast;
new Cast(string|Expression $expression, 'int'|'bigint'|'float'|'double' $type)
Invoice::select([
new Alias(new Cast('invoice_number', 'int')),
])->get();…To use the case-when in a Select wrap it in an Alias:
…………You can use these expressions directly with Laravel's where() method:
BlogVistis::where(new Equal('url', new Value('/exam\'ple1')))->get()…use Tpetry\QueryExpressions\Function\Conditional\{
Coalesce, Greatest, Least
};
use Tpetry\QueryExpressions\Language\Alias;
new Coalesce(array $expressions);
new Greatest(array $expressions);
new Least(array $expressions);
// SELECT GREATEST(published_at, updated_at, created_at) AS last_modification FROM blog_articles
BlogArticle::select([
new Alias(new Greatest('published_at', 'updated_at', 'created_at'), 'last_modification')
])
->get();use Tpetry\QueryExpressions\Function\Math\{
Abs,
};
new Abs(string|Expression $expression);use Tpetry\QueryExpressions\Function\String\{
Concat, Lower, Upper, Uuid4
};
new Concat(array $expressions);
new Lower(string|Expression $expression);
new Upper(string|Expression $expression);
new Uuid4();
Schema::table('users', function (Blueprint $table): void {
$table->uuid()->default(new Uuid4())->unique();
});Warning The
Uuid4expression is not available for all database versions. With PostgreSQL you need at least v13 and with MariaDB at least v10.10.
…Please see CHANGELOG for more information on what has changed recently.
The MIT License (MIT). Please see License File for more information.
暂无开放 Issues,或尚未同步最近议题。