laravel 的where 语句好弱,如何改进使其适应多条件?

larvel 的where语句遇上多个条件时,一大堆where,感觉好弱,如何改进
已邀请:

xiaoluyouyue

赞同来自: 长岛冰茶

闭包,回调都没有问题
like this:
$list = DB::table($this->table)
->join('user', 'user.id', '=', $this->table . '.user_id')
->where(function($query) use($data) {
if (!empty($data['order_sn'])) {
$query->where($this->table . '.order_sn', 'like', '%' . $data['order_sn'] . '%');
}
})
->where(function($query) use($data) {
if (isset($data['order_status']) && is_numeric($data['order_status'])) {
$query->where($this->table . '.order_status', '=', $data['order_status']);
}
})
->where(function($query) use($data) {
if (isset($data['ship_status']) && is_numeric($data['ship_status'])) {
$query->where($this->table . '.ship_status', '=', $data['ship_status']);
}
})
->where(function($query) use($data) {
if (isset($data['pay_status']) && is_numeric($data['pay_status'])) {
$query->where($this->table . '.pay_status', '=', $data['pay_status']);
}
})
->where(function($query) use($data) {
if (!empty($data['start_time']) && !empty($data['end_time'])) {
$query->where($this->table . '.update_time', '>=', $data['start_time'])
->where($this->table . '.update_time', '<=', $data['end_time']);
} else if (!empty($data['start_time'])) {
$query->where($this->table . '.update_time', '>=', $data['start_time']);
} else if (!empty($data['end_time'])) {
$query->where($this->table . '.update_time', '<=', $data['end_time']);
}
})
->where($this->table . '.drugstore_id', '=', 0)
->where($this->table . '.user_id', '!=', 0)
->select($this->table . '.id as id',
$this->table . '.order_sn as order_sn',
$this->table . '.order_status as order_status',
$this->table . '.ship_status as ship_status',
$this->table . '.pay_status as pay_status',
$this->table . '.total_paid as total_paid',
$this->table . '.update_time as update_time',
'user.nickname as name',
'user.mobile as phone',
$this->table . '.is_remittance as is_remittance')
->orderBy($sortby, $sortvalue)
->take($data['pageSize'])
->skip($data['pageNo'] > 1 ? ($data['pageNo'] - 1) * $data['pageSize'] : 0)
->get();

admin

赞同来自:

可以传数组

Heishy

赞同来自:

不弱呢,可以传Callable

要回复问题请先登录注册