bigdropinc/laravel-simple-search

dev-master 2020-01-27 16:01 UTC

This package is not auto-updated.

Last update: 2024-09-17 15:29:44 UTC


README

#Laravel Simple Search

##要求

在继续之前,请确保已安装所有依赖项

使用Composer拉取包

通过composer安装此扩展是首选方法。

运行以下命令之一:

composer require bigdropinc/laravel-simple-search "1.0.0"

或者

"bigdropinc/laravel-simple-search": "1.0.0"

将以下内容添加到您的composer.json文件的require部分。

使用方法

扩展安装后,只需在您的代码中简单使用即可:

UserSearch::apply(User:class, request()->all());

过滤器

所有应使用的属性均在fillable属性中描述

protected $fillable = [
   'id',
   'first_name',
   'last_name',
];

默认情况下,对每个属性应用的条件为=

示例

protected $fillable = [
   'id',
   'name' => 'first_name', //alias:  name - search attr, first_name - db attr
   'last_name',
];

等价于

User::where('first_name', $firstNameValue)
   ->where('last_name', $lastNameValue)
   ->where('id', $userIdValue)

自定义过滤器

pulic function id($value) {
   $this->query->where('id', '>', $value);
}

类型转换

使用默认的Laravel属性转换。默认使用string类型转换

protected $cast = [
  'id' => 'integer',	
];

排序

默认排序

protected $defaultSort = 'first_name';

按first_name升序排序: sort=first_name

按first_name降序排序: sort=-first_name | 开头带有连字符(-