繁体   English   中英

Laravel 选择左连接新数组索引中的所有列

[英]Laravel select left join all columns in new array index

我在 Laravel 查询中有一个问题,并在谷歌中搜索,但找不到

这是我的查询:

$data = DB::table('event')
        ->where('TU', '!=', null)
        ->where('TM', '!=', 180)
        ->leftJoin('market', 'event.event_id', '=', 'market.event_id')
        ->select([
            'event.event_id as id',
            'TU',
            DB::raw('(TM*60+TS) as elapsed'),
            'markets' => 'market.*'
        ])
        ->orderBy('event.time', 'ASC')
        ->get();

我的结果是这样的:

Collection {#380 ▼
  #items: array:3 [▼
    0 => {#381 ▼
      +"id": 629
      +"TU": 0
      +"elapsed": 1200
      +"default": 1
      +"group_name": "other"
      +"api_updated_at": null
      +"updated_at": "2020-01-31 11:16:11"
    }
    1 => {#382 ▶}
    2 => {#385 ▶}
  ]
}

我希望将市场表中的所有列推入一个索引中作为结果

像那样 :

Collection {#380 ▼
  #items: array:3 [▼
    0 => {#381 ▼
      +"id": 629
      +"TU": 0
      +"elapsed": 1200
      +"markets": array:3 [▼
        0 => {#382 ▼
          +"default": 1
          +"group_name": "other"
          +"api_updated_at": null
          +"updated_at": "2020-01-31 11:16:11"
        }
        1 => {#383 ▶}
        2 => {#384 ▶}
      ]
    }
  ]
}

请帮我在 Laravel 中编辑查询

您不能在选择中使用一个字段而不是一个表的多个字段。 你可以尝试使用

->select([
        'event.event_id as id',
        'TU',
        DB::raw('(TM*60+TS) as elapsed'),
        'market.*'
    ])

或使用with('market')而不是leftjoin

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM