首页 > 解决方案 > 删除现有的 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()

标签: laravelpostgresql

解决方案


如果您在添加 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();
...

推荐阅读