Search by

linron / think-filter

Lynron

A package that makes ThinkPHP8 model search more elegant.

Package info

github.com/Linron-Foo/think-filter

pkg:composer/linron/think-filter

Statistics

Installs: 6

Dependents: 0

Suggesters: 0

Stars: 2

1.0.0 2025-08-25 10:06 UTC

This package is auto-updated.

Last update: 2026-10-19 18:53:11 UTC


README

特性介绍

  • 让THINKPHP8模型搜索更优雅

安装

composer require linron/think-filter

用法

创建过滤器

<?php

namespace app\filter;

use linron\thinkfilter\QueryFilter;
use think\db\BaseQuery;

class UserListFilter extends QueryFilter
{

    public function name(string $name): BaseQuery
    {
        return $this->builder->whereLike('name', "%{$name}%");
    }

    public function age(int $age): BaseQuery
    {
        return $this->builder->where('age', $age);
    }
}

模型中使用

<?php

namespace app\model;

use linron\thinkfilter\src\traits\FilterTrait;
use think\Model;

class UserModel extends Model
{
    use FilterTrait;

    protected string $table = 'zl_user';
}
<?php

namespace app\service;

use app\domain\UserListSearchDto;
use app\filter\UserListFilter;
use app\model\UserModel;

class UserService
{

    public function getUserList(UserListSearchDto $searchDto): array
    {
        $args = [
            "name" => "测试bom123",
            "age" => 18,
        ];
        $users = UserModel::modelFilter($args, UserListFilter::class)->paginate(['page' => 1, 'list_rows' => 10]);
        return [
            "code" => 200,
            "msg" => "Ok",
            "data" => $users
        ];
    }
}