平舟/laravel-query-filter

v1.0.1 2022-07-21 15:05 UTC

This package is auto-updated.

Last update: 2024-09-09 12:22:11 UTC


README

安装

您可以使用 Composer 将此库添加为本地、按项目依赖项到您的项目中

composer require pianzhou/laravel-query-filter

数据库查询过滤器

配置 app.php

Pianzhou\Laravel\Query\Filter\ServiceProvider::class,

示例:

// demo one
DemoModel::filter($request->get('name'), function(int $value) {
        $this->where('name', 'like', $value);
    }, \Pianzhou\Laravel\Query\Filter\Filter::MODE_NULL);

// demo two
DemoModel::filters($request->only('column1', 'column2', 'column3'))->get();

// demo three
DemoModel::filters($request->only('name'), function (\Pianzhou\Laravel\Query\Filter\Filter $filter) {
    $filter->where('name', 'like')
        ->where('nickname', 'like');
})->get();

// demo four
DemoModel::filters($request->only('name'), function (\Pianzhou\Laravel\Query\Filter\Filter $filter) {
    $filter->when('name', function (int $value) {
            $this->where('name', 'like', $value);
        })
    ->when('nickname', function (string $value) {
        $this->where('nickname', 'like', $value);
    }, \Pianzhou\Laravel\Query\Filter\Filter::MODE_NULL);
})->get();