mihai-valentin/laravel-order-by-field
最新稳定版本:v1.2.0
Composer 安装命令:
composer require mihai-valentin/laravel-order-by-field
包简介
A simple Laravel `Query\Builder` extension that adds MySQL-like "order by field" feature
README 文档
README
This package provides a few Laravel Query\Buider marco in order to implement MySQL-like order by filed(...) feature.
Installation
Get the package
composer require mihai-valentin/laravel-order-by-field
Usage
use \Illuminate\Support\Facades\DB; // Order records by a column asc DB::table('table_name')->orderByField('column', ['first', 'second', 'third']); // Order records by a column desc DB::table('table_name')->orderByField('column', ['first', 'second', 'third'], 'desc'); DB::table('table_name')->orderByFieldDesc('column', ['first', 'second', 'third']);
How it works
For the MySQL macro will generate a native order by field(...) expression. For all other drivers order clause will be
implemented using case predicate.
Using MySQL
use \Illuminate\Support\Facades\DB; // Before DB::table('table_name')->orderByRaw("field(`column`, 'first', 'second', 'third')"); // With macro DB::table('table_name')->orderByField('column', ['first', 'second', 'third']);
Using Postgresql, Sqlite
use \Illuminate\Support\Facades\DB; // Before DB::table('table_name')->orderByRaw(" case when \"column\"='first' then 1 when \"column\"='second' then 2 when \"column\"='third' then 3 else 0 end "); // With macro DB::table('table_name')->orderByField('column', ['first', 'second', 'third']);
PhpStorm autocomplete
You can create an _ide_helper file to tell your IDE about new methods. The helper file can look like this
<?php namespace Illuminate\Contracts\Database\Query { use MihaiValentin\LaravelOrderByFiled\OrderByFieldServiceProvider; /** * @method Builder orderByField(string $column, array $order, string $direction = 'asc') * @method Builder orderByFieldDesc(string $column, array $order) * * @see OrderByFieldServiceProvider */ interface Builder {} } namespace Illuminate\Database\Eloquent { /** * @method Builder orderByField(string $column, array $order, string $direction = 'asc') * @method Builder orderByFieldDesc(string $column, array $order) * * @see OrderByFieldServiceProvider */ class Builder implements BuilderContract {} }
You can find it here
Tests
You can run tests using the make
# Run tests, code static analysis and cs fixer make test
# Run phpunit tests
make phpunit
Build
You can build the package using the make
# Install composer dependencies and run tests
make
Code of Conduct
In order to ensure that the community is welcoming to all, please review and abide by the Code of Conduct.
Contributing
Please see CONTRIBUTING.md for details.
License
The MIT License (MIT). Please see License File for more information.
统计信息
- 总下载量: 14.22k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 1
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-01-16