php Laravel如何在数组中放置具有相同日期的相关列

3ks5zfa0  于 2023-04-28  发布在  PHP
关注(0)|答案(2)|浏览(88)

我现在正在调用几个嵌套关系。但是我尝试做的是在一个数组中获得具有相同“measured_at”日期的所有列,而不是将它们分散。
我得到的结构的一个简单例子:

{
    "id": 1,
    "user_body_sizes": [
        {
            "id": 23,
            "measured_at": "2023-04-04"
        },
        {
            "id": 24,
            "measured_at": "2023-04-04"
        },
        {
            "id": 26,
            "measured_at": "2023-03-14"
        },
        {
            "id": 29,
            "measured_at": "2023-03-14"
        }
    ]
}

这就是我真正想要实现的:

{
    "id": 1,
    "user_body_sizes": [
        [
            {
                "id": 23,
                "measured_at": "2023-04-04"
            },
            {
                "id": 24,
                "measured_at": "2023-04-04"
            }
        ],
        [
            {
                "id": 29,
                "measured_at": "2023-03-14"
            },
            {
                "id": 26,
                "measured_at": "2023-03-14"
            }
        ]
    ]
}

所以基本上我希望具有相同“measured_at”日期的数据在同一个数组中。
所以在Laravel中,这是我现在获取数据的代码:

public function GetCustomerBodySize($customerId, $month, $year)
    {
        $customerUserId = Customer::findOrFail($customerId)['user'];

        return CustomerBodySize::with([ 'customer_data.user_body_sizes.body_parts','customer_data.user_body_sizes' => function ($q) use ($month, $year){
            $q->whereMonth('measured_at', '=', $month);
            $q->whereYear('measured_at', '=', $year);
            $q->orderBy('measured_at');
        }])
            ->whereRelation('customer_data', 'id', $customerUserId)
            ->first();
    }

以下是我的亲戚:

// This is in my CustomerBodySize model
function customer_data(){
        return $this->belongsTo(User::class, 'customer');
    }

function body_parts(){
        return $this->belongsTo(BodyParts::class, 'body_part');
    }

// This is in my User model
public function user_body_sizes(){
        return $this->hasMany(CustomerBodySize::class, 'customer');
    }
oxf4rvwz

oxf4rvwz1#

您可以使用Laravel's Collection的groupBy()方法按measured_at日期对user_body_sizes进行分组
1.我们首先使用get()而不是first()使用给定过滤器fetching all相关CustomerBodySize记录
1.然后用groupBy()方法将它们的measured_at日期进行分组
1.最后,我们使用map()返回一个数组,每个nested数组包含共享相同measured_at dateCustomerBodySize记录。我们还使用values()resulting collection中删除grouping keys
1.如果没有匹配给定过滤器的记录,则返回null

修改代码在这里

public function GetCustomerBodySize($customerId, $month, $year)

{

$customerUserId = Customer::findOrFail($customerId)['user'];

$bodySizes = CustomerBodySize::with([
    'customer_data.user_body_sizes.body_parts',
    'customer_data.user_body_sizes' => function ($q) use ($month, $year) {
        $q->whereMonth('measured_at', '=', $month);
        $q->whereYear('measured_at', '=', $year);
        $q->orderBy('measured_at');
    }
])
->whereRelation('customer_data', 'id', $customerUserId)
->get();

$groupedBodySizes = $bodySizes->groupBy(function ($item) {
    return $item->measured_at;
});

$result = $groupedBodySizes->map(function ($item) {
    return $item->values();
})->values();

return $result->isEmpty() ? null : $result->first();
}
nqwrtyyt

nqwrtyyt2#

public function GetCustomerBodySize($customerId, $month, $year)
    {
        $customerUserId = Customer::findOrFail($customerId)['user'];

        $data = CustomerBodySize::with([ 'customer_data.user_body_sizes.body_parts','customer_data.user_body_sizes' => function ($q) use ($month, $year){
            $q->whereMonth('measured_at', '=', $month);
            $q->whereYear('measured_at', '=', $year);
            $q->orderBy('measured_at');
        }])
            ->whereRelation('customer_data', 'id', $customerUserId)
            ->first();

        // this did the job for me
        $data = $data->toArray();

        $groupedUserBodySizes = collect($data['customer_data']['user_body_sizes'])->groupBy('measured_at')->values()->toArray();

        $data['customer_data']['user_body_sizes'] = $groupedUserBodySizes;

        return $data;
    }

相关问题