gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 15:11:58 25 4
gpt4 key购买 nike

我在 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 ▶}
]
}

i want all columns from market table pushed in one index in result

像这样:

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 中不能使用一个字段代替一个表的多个字段。你可以尝试使用

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

或使用with('market')代替leftjoin

关于php - Laravel 选择左连接新数组索引中的所有列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60000418/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com