laravel - 单个查询是否可以处理 if 语句
问题描述
单个查询可以做到这一点吗
Schedule::where('location_id', $this->location_id) //if result found do nothing else
// but if no result found then evaluate
wherisNull(location_id)
解决方案
有时,这些步骤可以避免多个条件查询。
- 将查询结果存储在变量中。结果是一个集合。
- 使用 where 和其他集合助手来约束集合。
单个查询和集合助手可以解决问题。
我就是这样做的。
查询
$appointment_at = $this->appointment_at;
if(request()->ismethod("Post") || request()->ismethod("Patch")) {
$schedules = Schedule::whereHas('dayTimes', function ($query) use ($appointment_at) {
$query->where('start_time', '<=', $appointment_at->format('H:m'))
->where('end_time', '>=', $appointment_at->format('H:m'));
})
->whereDate('start_date', '<=', $appointment_at)
->whereDate('end_date', '>=', $appointment_at)
->get();
}
收集助手的使用
if(empty($this->location_id) && empty($this->service_id) && empty($this->user_id)) {
$schedule = $schedules;
}
else if(!empty($this->location_id) && empty($this->service_id) && empty($this->user_id)) {
$schedule = $schedules->where('location_id', $this->location_id);
if(! $schedule->count()) {
$schedule = $schedules->whereNull('location_id');
}
}
else if(empty($this->location_id) && !empty($this->service_id) && empty($this->user_id)) {
$schedule = $schedules->where('service_id', $this->service_id);
if(! $schedule->count()) {
$schedule = $schedules->whereNull('service_id');
}
}
else if(empty($this->location_id) && empty($this->service_id) && !empty($this->user_id)) {
$schedule = $schedules->where('user_id', $this->user_id);
if(! $schedule->count()) {
$schedule = $schedules->whereNull('user_id');
}
}
else if(!empty($this->location_id) && !empty($this->service_id) && empty($this->user_id)) {
$schedule = $schedules->where('location_id', $this->location_id)->where('service_id', $this->service_id);
if(! $schedule->count()) {
$schedule = $schedules->where('location_id', $this->location_id)->whereNull('service_id');
}
if(! $schedule->count()) {
$schedule = $schedules->whereNull('location_id');
}
}
else if(!empty($this->location_id) && empty($this->service_id) && !empty($this->user_id)) {
$schedule = $schedules->where('location_id', $this->location_id)->where('user_id', $this->user_id);
if(! $schedule->count()) {
$schedule = $schedules->where('location_id', $this->location_id)->whereNull('user_id');
}
if(! $schedule->count()) {
$schedule = $schedules->whereNull('location_id');
}
}
else if(empty($this->location_id) && !empty($this->service_id) && !empty($this->user_id)) {
$schedule = $schedules->where('service_id', $this->service_id)->where('user_id', $this->user_id);
if(! $schedule->count()) {
$schedule = $schedules->where('service_id', $this->service_id)->whereNull('user_id');
}
if(! $schedule->count()) {
$schedule = $schedules->whereNull('service_id');
}
}
else if(!empty($this->location_id) && !empty($this->service_id) && !empty($this->user_id)) {
$schedule = $schedules->where('location_id', $this->location_id)->where('service_id', $this->service_id)->where('user_id', $this->user_id);
if(! $schedule->count()) {
$schedule = $schedules->where('location_id', $this->location_id)->where('service_id', $this->service_id)->whereNull('user_id');
}
if(! $schedule->count()) {
$schedule = $schedules->where('location_id', $this->location_id)->whereNull('service_id')->whereNull('user_id');
}
if(! $schedule->count()) {
$schedule = $schedules->whereNull('location_id');
}
}
推荐阅读
- xslt - XSL:检索标签值
- shell - awk:负指数未被正确解释
- ssis - 使用 Newtonsoft.JSON 切换到 2016 目标服务器时,SSIS 脚本任务失败
- c++ - 让函数在不指定模板参数的情况下获取指向模板对象的指针
- c++ - C++ 方法参数中的 & 符号:SomeType& param1 和 SomeType ¶m1 之间的差异
- javascript - 当我为 gatsby-starter-gcn 运行 gatsby develop 时,如何修复错误 #10123?
- c++ - 该对象具有与使用向量的成员函数不兼容的类型限定符
- android-intent - Dialogflow 基于规则的语法匹配
- html - SVG使用元素href引用shadowRoot之外的元素?
- python - 熊猫根据另一个数据框的 2 列添加新列