little-superman / laravel-request-params
处理请求参数
v0.0.1
2019-08-02 03:50 UTC
Requires
- php: >=7.1.3
- illuminate/config: 5.8.*
- illuminate/database: 5.8.*
- illuminate/routing: 5.8.*
- illuminate/support: 5.8.*
- symfony/routing: ^4.2
This package is auto-updated.
Last update: 2024-09-07 16:39:09 UTC
README
安装
composer require little-superman/laravel-request-params
Laravel
laravel >= 5.5
ServiceProvider 将自动加载
其他版本
在 config/app.php 中添加 LittleSuperman\RequestParams\Providers\ConsoleServiceProvider::class, LittleSuperman\RequestParams\Providers\ConfigServiceProvider::class
[
'providers' => [
...,
LittleSuperman\RequestParams\Providers\ConfigServiceProvider::class,
LittleSuperman\RequestParams\Providers\ConsoleServiceProvider::class,
],
];
命令行
创建模型
php artisan make:modle 模型名称
创建请求资源
php artisan mkae:request 资源名称
字段说明
格式
{
"where": {
"userId|name[!]":1,
"|userId[~]":[1,3,4],
"userId":1,"userId[!]":1
},
"order":{
"userId":"desc"
},
"page":1,
"limit":10,
}
字段说明
where 用于数据过滤
order 用于排序
page 分页
limit 条数
where 条件字段限制
[
'whereSymbols' => [
//key => '对应前端传递值'
'and' => '&',
'or' => '|',
'equal' => '=',
'notEqual' => '!',
'greater' => '>',
'less' => '<',
'in' => '-',
'notIn' => '!-',
'between' => '<>',
'notBetewen' => '><',
'like' => '~',
'notLike' => '!=',
],
];
order 字段限制
[
'orderSymbols' => [
//key => '对应前端传递值'
'asc' => 'asc',
'desc' => 'desc'
],
];
配置文件位置: resources/config/dynamicQuery.php
##使用
配置Model
-
在模型中引入
LittleSuperman\RequestParams\DynamicQuery\Query -
模型中添加
$searchField, key 对应前端使用的字段 value 对应数据库使用字段
$searchField = [
'userId' => 'id',
];
配置样式
<?php namespace App\Models; use Illuminate\Database\Eloquent\Model; use LittleSuperman\RequestParams\DynamicQuery\Query; class User extends Model { //引入改特征 use Query; /** * 搜索字段 * * @var array */ protected $searchField = [ //允许使用的字段 //前端使用的字段 => 数据库字段 'userId' => 'id', ]; }
开启动态查询
在模型后面调用queryable方法来开启动态查询
User::queryable()->paginate();
加载其他模型搜索字段
User::queryable(User::class, ...)->get();