laravel - 删除现有的 Where 子句
问题描述
我想在某些情况下删除 where 子句:
$q = Thread::with('comments')->take(10);
if(strlen($input) < 4){
$result = $q->where('title', '~', "^$input$")->get();
if($result->isNotEmpty()){
return $result;
}
// if was empty:
}
// How to clean the where clause from the above here? because it affect the query below:
$result = $q->where('title', 'like', "%$input%")->get();
问题是如果数据为空,第一个 where 子句会影响第二个 where 子句,我如何在需要时删除现有的 where 子句?在我的情况下也newQuery()
不起作用。
请注意,我在 postgres~
和 'like'中使用了两个单独的语句
类似于where 子句的reorder()
解决方案
如果您在添加 where 之前克隆它与删除 where 相同
...
if(strlen($input) < 4){
$result = (clone $q)->where('title', '~', "^$input$")->get();
if($result->isNotEmpty()){
return $results;
}
}
$result = $q->where('title', 'like', "%$input%")->get();
...
推荐阅读
- c++ - Gtest 和 Gmock 教程和文档站点/博客
- python - Nifi ExecuteScript / 尾文件
- javascript - 在 Web Inspector 中找不到 HttpOnly Cookie
- php - JWT 使用 Firebase 在没有 cookie 的情况下保持登录
- r - 循环R中的2个数据集以匹配一个日期集中所有行的值与另一个日期集中只有一列
- java - spring 中的外部属性文件
- c++ - 输出运算符如何知道字符数组的大小?
- haskell - stack-upgrade:将 1.5.1 堆栈到最新版本
- python - 我的 CGI 如何访问 firestore 数据库?
- c++ - 如何在c ++中找到数字的最大因子(除了它本身)