dpt62283 2014-05-28 17:37
浏览 24
已采纳

Laravel Query Builder“选择”奇怪的行为

I have a problem with the following code:

public function index() {
    $qObj = \DB::table('customers as c')
            ->join('companies_has_customers as chc', 'chc.comhc_cus_id', '=', 'c.cus_id')
            ->join('cuscategory as cc', 'chc.comhc_cca_id', '=', 'cc.cca_id')
            ->join('transactions as t','t.trn_cus_id', '=','c.cus_id')
            ->select('t.trn_id');  

    $qObj->where('comhc_com_id', '=', $this->companyId);


    $res = array(
        'count' => $qObj->count(),
        'items' => $qObj->get()
    );

    //print_r($this->showLastQuery());

    return parent::prepareResponse($res, 200, 'customers');
}

And this piece of code produces following SQL query:

select * from `customers` as `c` 
inner join `companies_has_customers` as `chc` on `chc`.`comhc_cus_id` = `c`.`cus_id`     
inner join `cuscategory` as `cc` on `chc`.`comhc_cca_id` = `cc`.`cca_id` 
inner join `transactions` as `t` on `t`.`trn_cus_id` = `c`.`cus_id` 
where comhc_com_id` = 1

QUESTION:

Why QueryBuilder produce select *... instead of select t.trn_id... as is requested in above syntax?

  • 写回答

2条回答 默认 最新

  • dsimib1625 2014-05-28 17:43
    关注

    Put it in $qObj->get(['t.trn_id']) like this:

    $res = array(
        'items' => $items = $qObj->get(['t.trn_id']),
        'count' => count($items) // count the cached $items
    );
    

    Instead of select ( Remove select('t.trn_id')).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题