firevel/filterable

为 Laravel Eloquent 模型提供的一个简单特性,允许您轻松地过滤查询。

0.0.9 2024-05-24 21:27 UTC

This package is auto-updated.

Last update: 2024-09-24 22:07:38 UTC


README

为 Laravel Eloquent 模型提供的一个简单特性,允许您轻松地过滤查询。

安装

composer require firevel/filterable

设置

  1. Filterable 特性添加到您的 Eloquent 模型中。
  2. 在您的模型中定义 $filterable 属性,指定哪些属性可以过滤及其类型。
use Firevel\Filterable\Filterable;

class User extends Model
{
    use Filterable;

    protected $filterable = [
        'user_id' => 'id',
        'name' => 'string',
        'created_at' => 'datetime',
        // ... other attributes ...
    ];
}

允许的过滤类型

  • 整数
  • 日期
  • 日期时间
  • id
  • 字符串
  • 关系
  • 布尔值
  • JSON
  • 数组

使用方法

在您的 Eloquent 查询上使用 filter() 范围应用过滤器。

// Fetch users with user_id equal to 5
$users = User::filter(['user_id' => ['=' => 5]])->get();

// Fetch users created after a certain date
$users = User::filter(['created_at' => ['>' => '2023-01-01']])->get();

// ... other filter combinations ...