首页 > 解决方案 > 在 laravel 查询生成器上的条件位置不返回任何结果

问题描述

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

$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}}

请帮助指出我在哪里弄错了。谢谢

标签: phplaravelpostgresqlquery-builder

解决方案


尝试改用这个:

$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();

推荐阅读