Laravel查询构建器cloneWithout方法无效问题及正确用法咨询
问题原因及解决方案
你用错了cloneWithout()的参数——这个方法需要传入Laravel查询构建器的组件别名,而不是自定义名称。orderBy对应的组件别名是'orderBy',不是'orders',所以传['orders']根本不会触发排序语句的移除。
正确用法示例
// 原查询 $query = DB::table('your_table') ->select(['id', 'status', 'supplier_id']) ->orderBy('id'); // 正确移除orderBy语句 $clonedQuery = $query->cloneWithout(['orderBy']); // 此时$clonedQuery生成的SQL不会包含ORDER BY id
常用查询组件别名参考
如果需要移除其他查询组件,对应别名如下:
- 字段选择:
select - 条件约束:
where - 排序:
orderBy - 分组:
groupBy - 聚合筛选:
having - 关联查询:
join
比如要同时移除select和orderBy:
$clonedQuery = $query->cloneWithout(['select', 'orderBy']);
内容的提问来源于stack exchange,提问作者Max Carvalho
相关产品推荐
相关产品推荐

