and48/table-filters

使Eloquent模型可过滤。

1.0.1 2023-07-28 09:34 UTC

This package is auto-updated.

Last update: 2024-09-26 15:11:39 UTC


README

制作Eloquent模型可过滤的包。

设置

Composer

通过Composer引入此包

composer require and48/table-filters

发布迁移

将包迁移文件发布到您的应用程序中。

php artisan vendor:publish --provider="AND48\TableFilters\TableFiltersServiceProvider" --tag="migrations"

运行迁移。

php artisan migrate

发布配置

将包配置文件发布到您的应用程序中。

php artisan vendor:publish --provider="AND48\TableFilters\TableFiltersServiceProvider" --tag="config"

查看配置文件 (config/filters.php) 并根据需要调整。

用法

为模型创建过滤器。

use AND48\TableFilters\Models\Filter;
...
User::addTableFilters([
    ['field' =>'id', 'type' => Filter::TYPE_NUMBER, 'caption' => 'ID'],
    ['field' =>'name', 'type' => Filter::TYPE_STRING, 'caption' => 'Name'],
    ['field' =>'birthday', 'type' => Filter::TYPE_DATE, 'caption' => 'Birthday'],
    ['field' =>'is_blocked', 'type' => Filter::TYPE_BOOLEAN, 'caption' => 'Is blocked'],
    ['field' =>'balance', 'type' => Filter::TYPE_NUMBER, 'caption' => 'Balance'],
    ['field' =>'status', 'type' => Filter::TYPE_ENUM, 'caption' => 'Status'],
    ['field' =>'parent_id', 'type' => Filter::TYPE_SOURCE, 'caption' => 'Parent user'],
]);
...
}

在您的 Eloquent 模型(s) 中使用 TableFilterable 特性。

获取您的过滤器。

$filters = User::tableFilterList(true, [
                'status' => ['new', 'verified', 'active', 'suspended']]);

获取源数据。

$filter = Filter::find($filter_id);
$source_data = $filter->sourceData($page, $search_query);

通过路由加载源数据。

route('filters.source_data', ['filter_id' => 1, 'query' => '*', 'page' => 2]);

过滤模型。

$filters = [
    ['id' => 1, 'operator' => '!=', 'values' => [2, 3]],
    ['id' => 2, 'operator' => '~', 'values' => ['and', 'dy']],
    ['id' => 3, 'operator' => '>=', 'values' => ['1986-06-06']],
    ['id' => 4, 'operator' => '=', 'values' => [false]],
    ['id' => 6, 'operator' => '=', 'values' => ['new', 'verified']],
    ['id' => 7, 'operator' => '!=', 'values' => []],
];
$users = User::tableFilter($filters)->get();

为了保存、更新或删除过滤器到存储(数据库表),请使用资源路由 filters.storages

与客户端库 table-filters-client 一起使用。