imzeali/filter-eloquent

一个优雅的ORM过滤器

1.5 2019-10-08 09:35 UTC

This package is auto-updated.

Last update: 2024-09-10 18:35:44 UTC


README

通过拼接查询字符串快速过滤数据

安装

composer install imzeali/filter-eloquent

支持的运算符

  ' '           // equal to
  'eq'          // equal to
  'ne'          // not equal to
  'gt'          // Is greater than
  'ge'          // great than and equal to
  'lt'          // less than
  'le'          // less than and equal to 
  'like'        // LIKE
  'in'          // IN
  'not_in'      // NOT IN

用法

$q 语法:{字段名}__{运算符}={查询条件}

$q 可以由客户端分割

基本查询:

$q = 'id__eq=100';
new Filter(new User(), $q)->filteredQuery();
//Equivalent to
User::where('id',1);

多个查询条件:

$q = 'id__gt=100,name__like=%baby%';
new Filter(new User(), $q)->filteredQuery();
//Equivalent to
User::where('id', ‘>’, ‘100’)->where('name','like','%baby%');

模型查询条件

$q = 'user.city__eq=Fuzhou';
new Filter(new Article(), $q)->filteredQuery();
//Equivalent to
Article::whereHas('user', function ($query){
    $query->where('city', '=', 'Fuzhou');
});