alex-kudrya / orderbyfield
SQL "ORDER BY FIELD()" 函数用于 Laravel 查询构建器或模型
v1.1
2023-12-08 20:16 UTC
Requires
- php: >= 8
- ext-pdo: *
- illuminate/collections: >= 10
- illuminate/support: >= 10
This package is not auto-updated.
Last update: 2024-09-28 00:02:29 UTC
README
这是一个具有 scope
函数的特质,适用于 Laravel 应用程序
安装
composer require alex-kudrya/orderbyfield
使用方法
示例
// app/Models/User.php
namespace App\Models;
use AlexKudrya\OrderByField\OrderByField;
class User extends Model
{
use OrderByField;
...
}
现在您可以使用 orderByField()
方法
$ordered_list = User::orderByField('role_id', [4,2])->get();
现在在 $ordered_list
中,所有用户都按照相同的顺序排序:首先是 role_id = 4 的用户,其次是 role_id = 2 的用户,然后是其他所有用户。