php - 在 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}}
请帮助指出我在哪里弄错了。谢谢
解决方案
尝试改用这个:
$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();
推荐阅读
- node.js - NodeJS中的异步/等待文件保存和Sharp图像处理问题
- vba - 如何在 VBA for Powerpoint 上使随机不重复?
- python - 无法在 VSCode 中使用 python
- apache-kafka - 获取KStream的最后一条记录
- python-3.x - Telegram - 如何阅读 Telegram Bot 发送的消息
- c - 请参阅扩展的 C 宏
- node.js - 带有 node-sass、autoprefixer 和 cssnano 的 npm
- android - SwipeRefreshLayout:得到 ACTION_MOVE 事件但没有活动指针 id
- r - 在 R 中删除 2000 年之前的年份
- javascript - Puppeteer - 单击没有输入 ID 的按钮