条件 where 在 laravel 查询生成器上没有返回结果

时间:2021-07-10 04:15:39

标签: php laravel postgresql query-builder

我正在尝试根据提供的参数进行有条件的 where 查询,但是,它不返回任何结果,而当我将整个查询直接放入查询构建器时,它确实返回了一个结果,因为表中有特定数据.这是我目前拥有的

$tasks = DB::table('customer_master')->select(DB::raw("customer_name,
    substring(birthday,1,4)||' - '||substring(birthday,5,2)||' - '||substring(birthday,7,2) as dob"));
    
if (isset($idCustomer)){
    $tasks->where('customer_id', $idCustomer);
}

$tasks->first();
return $tasks;

返回如下:

{"data":{"connection":{},"grammar":{},"processor":{},"bindings":{"select":[],"from":[],"join":[],"where":["0001108032"],"groupBy":[],"having":[],"order":[],"union":[],"unionOrder":[]},"aggregate":null,"columns":[{}],"distinct":false,"from":"customer_master","joins":null,"wheres":[{"type":"Basic","column":"customer_id","operator":"=","value":"0001108032","boolean":"and"}],"groups":null,"havings":null,"orders":null,"limit":1,"offset":null,"unions":null,"unionLimit":null,"unionOffset":null,"unionOrders":null,"lock":null,"operators":["=","<",">","<=",">=","<>","!=","<=>","like","like binary","not like","ilike","&","|","^","<<",">>","rlike","not rlike","regexp","not regexp","~","~*","!~","!~*","similar to","not similar to","not ilike","~~*","!~~*"],"useWritePdo":false}}

请帮忙指出我哪里出错了。谢谢

1 个答案:

答案 0 :(得分:0)

尝试改用这个:

$tasks =  DB::table('customer_master')
        ->select(DB::raw("customer_name,
        substring(birthday,1,4)||' - '||substring(birthday,5,2)||' - '||substring(birthday,7,2) as dob"));
        
if (isset($idCustomer)){
    $tasks->where('customer_id', $idCustomer);
}

return $tasks->first();