Мне нужно сгруппировать следующие строки в зависимости от типа, но мне нужно, чтобы Коллекция имела только элемент, а не массив с 1 элементом внутри.
Примеры строк базы данных:
id location_id type section_id content
0 2 telephone 1 123135145
1 2 mobile 1 245446546
Ожидаемый результат:
[
'telephone' => [
'id' => 0,
'location_id' => 2,
'type' => 'telephone',
'section_id' => 1,
'content' => 123135145
],
'mobile' => [
'id' => 1,
'location_id' => 2,
'type' => 'mobile',
'section_id' => 1,
'content' => 245446546
]
]
Фактический результат:
[
'telephone' => [
0 => [
'id' => 0,
'location_id' => 2,
'type' => 'telephone',
'section_id' => 1,
'content' => 123135145
]
],
'mobile' => [
0 => [
'id' => 1,
'location_id' => 2,
'type' => 'mobile',
'section_id' => 1,
'content' => 245446546
]
]
]
Вот мой красноречивый запрос
$contactDetails = $this->contactDetails()->where([ 'section_id' => 1 ])->get()->groupBy('type');
Метод contactDetails()
- это отношение:
public function contactDetails(){
return $this->hasMany(WebsiteLocationContactDetails::class);
}