Php 雄辩的a组关系

Php 雄辩的a组关系,php,laravel,Php,Laravel,我有我的主要位置模型,这个模型有很多联系模型。但是,接触模型需要按其类型分组 我该怎么做呢 我试图在我的位置上绘制地图,但这似乎不起作用 $data = WebsiteLocation::query() ->where(['website_id' => $website['id']]) ->orderBy('order') ->get() ->keyBy('vehicle_stock_location'); $primaryInde

我有我的主要位置模型,这个模型有很多联系模型。但是,接触模型需要按其类型分组

我该怎么做呢

我试图在我的位置上绘制地图,但这似乎不起作用

$data = WebsiteLocation::query()
    ->where(['website_id' => $website['id']])
    ->orderBy('order')
    ->get()
    ->keyBy('vehicle_stock_location');

$primaryIndex = $data->map(function($location) {
    return (int)$location->primary_path === 1;
})->search(true) ?: array_keys($data->toArray())[0];

$data = $data->map(function($location) {
    $location->contact = $location->contact->groupBy('type');
    return $location;
});
这是我的位置模型:

<?php

namespace App\Models\Locations;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;

class WebsiteLocation extends Model
{

    protected $with = [ 'contact' ];

    public function contact(){
        return $this->hasMany(WebsiteLocationContactDetails::class)->orderBy('order', 'type');
    }

}
我所期望的是:

[
    0 => [
        'location_name' => 'test1',
        'contact' => [
            'telephone' => [
                0 => [
                    'type' => 'telephone',
                    'content' => '000000'
                ],
                1 => [
                    'type' => 'telephone',
                    'content' => '004000'
                ],
            ]
            'mobile' => [
                0 => [
                    'type' => 'mobile',
                    'content' => '000234'
                ]
            ]
        ]
    ],
    1 => [
        'location_name' => 'test1',
        'contact' => [
            'telephone' => [
                0 => [
                    'type' => 'telephone',
                    'content' => '000000'
                ]
            ]           
        ]
    ]
]
这个怎么样:

<?php 
$data = WebsiteLocation::where(['website_id' => $website['id']])
    ->with('contacts')
    ->orderBy('order')
    ->get();

foreach($data as $record){
  $contacts = $record->contacts->groupBy('type);
}

我有两个电话号码,其中一个位置有一个手机,上面的代码似乎删除了其中一个电话号码。请看我更新后的帖子,结果只是使用关系获取所有联系人类型,然后获取这些类型的内容详细信息,不过还是要谢谢
<?php 
$data = WebsiteLocation::where(['website_id' => $website['id']])
    ->with('contacts')
    ->orderBy('order')
    ->get();

foreach($data as $record){
  $contacts = $record->contacts->groupBy('type);
}