当前位置:网站首页>when的多条件查询
when的多条件查询
2022-07-27 06:35:00 【Doc_ACwhite】
when的条件查询格式:
>when($A,function ($string)use($B){
return $string->where('id',"$B");}
when中第一个参数$A用来判断$A是否存在,不存在直接return退出查询,存在则走function
function中的第一个参数string代表的是你的查询整体,命名随意($strlen,$inter,$abc等等)
use中是你需要代入的查询条件
举个栗子:
public function classification(Request $request){
#每页条数
$size = 100;
#当前页
$page = 1;
#分类id
$kind_id = $request->get('kind_id');
#分类状态
$kind_status = $request->get('kind_status');
#keywords需要检索的钥
$keywords = $request->get('keywords');
$info = recursiveCategory(Kinds::with('image')
->when($kind_id,function ($string)use($kind_id){
return $string->where('id',"$kind_id");
})->when($kind_status,function ($string)use($kind_status){
return $string->where('kind_status',$kind_status);
})->when($keywords,function ($string)use($keywords){
return $string->where('kind_name','like',"$keywords%");
})->offset(($page-1)*$size))->limit($size)->get();
return succeed(['kindInfo' => $info],'分类展示成功');
}
边栏推荐
- Internal class -- just read this article~
- 35. Search Insert Position 搜索插入位置
- Basic functions and collections of guava
- Cadence(十一)丝印调整和后续事项
- How to submit C4d animation to cloud rendering farm for fast rendering?
- 【golang学习笔记2.1】 golang中的数组中的排序和查找
- 杂谈:最近好多朋友谈出国……
- 海康h9摄像头用xshell无法连接(没有启用ssh)
- (2022 Niuke multi school III) a-ancestor (LCA)
- Simple rotation chart
猜你喜欢

(2022牛客多校三)A-Ancestor(LCA)

杂谈:跟女儿聊为啥要学好文化课

VLAN trunk实验

Cadence(十一)丝印调整和后续事项

SQLite 常用功能整合

Usage of string class

ClickHouse 笔记1 | 简介、特点 | 基于CentOS7系统的安装与使用 | 常用数据类型 | MergeTree 表引擎 | SQL操作

在kettle中快速更新一个字段中的信息

sql-labs SQL注入平台-第1关Less-1 GET - Error based - Single quotes - String(基于错误的GET单引号字符型注入)

(2022牛客多校三)J-Journey(dijkstra)
随机推荐
Drools (5): drools advanced syntax
No.0 training platform course-2. SSRF Foundation
Array method and loop in JS
优炫数据库主要线程有哪些?
单臂路由(讲解+实验)
Gossip: talk with your daughter about why you should learn culture lessons well
12. Integer to Roman
简单的轮播图
Logcat tool
想sink 到 redis-hash 里面 把 对象的属性和值都写进去 ,大佬们有Demo 吗?
Use of tigervnc
Use shell to calculate the sum of numbers in text
C语言实现猜数字小游戏项目实战(基于srand函数、rand函数,Switch语句、while循环、if条件判据等)
Pg_relation_size 问题
请教大佬们一个问题,pgsqlcdc任务运行一段时间就不能监测变化了,重启就可以了,这个该从哪方面入
jjwt 生成token
(2022牛客多校三)A-Ancestor(LCA)
Binary tree -- natural search semantics (1) Basics
使用pip命令切换不同的镜像源
DRConv-pytorch改称输出和输入一样的尺寸