Laravel groupBy in collection not working

469 Views Asked by At

I am trying to group the result with the status name inside. I am not doing groupBy after collection because I am using resource to return data, there are some logic inside resource. Grouping data before using get() gives an error because it needs table joining. So I have the following code:

return TicketResource::collection(
    $data
)->collection->groupBy('ticket_detail.status.name');

And following is the output. if I do ->collection()->groupBy('appliance.name') it groups but not with the above one.

{
    "id": 1,
    "ticket_no": "76zx3c02vv",
    "title": "Et veniam.",
    "date": "yesterday",
    "problem": "Illo nam distinctio quia qui.",
    "ticket_price": "",
    "price": 597.17,
    "exit_condition": null,
    "note": "Esse dolor fuga harum.",
    "province": {
        "id": 90,
        "name": "Sud Sardegna"
    },
    "lat": null,
    "lng": null,
    "client": {
        "id": 46,
        "name": "Kelvin Murray",
        "phone": "+1 (484) 474-9588"
    },
    "zip_code": {
        "id": 4507,
        "zip_code": "90043"
    },
    "brand": {
        "id": 3,
        "name": "Samsung"
    },
    "appliance": {
        "id": 1,
        "name": "Refrigerator"
    },
    "user": {
        "id": 3,
        "name": "Lorna Lubowitz",
        "email": "[email protected]",
        "fiscal_code": "2022-03-15 16:08:06",
        "tel": "+1 (747) 700-9310",
        "image": null,
        "user_type": "buyer",
        "status": "suspended",
        "otp_code": null,
        "otp_expiry_time": null,
        "email_verified_at": "2022-03-15T10:23:06.000000Z",
        "device_token": null,
        "last_login_at": null,
        "created_at": "2022-03-15T10:23:06.000000Z",
        "updated_at": "2022-03-15T10:23:06.000000Z"
    },
    "expiry_date": "1982-08-17",
    "created_at": "2022-03-15T10:23:07.000000Z",
    "updated_at": "2022-03-15T10:23:07.000000Z",
    "buyer_tickets": [
        {
            "id": 2,
            "ticket_id": 1,
            "buyer_id": 11,
            "premium": 0,
            "reason_for_cancellation": null,
            "cancel_time": "2022-03-23 13:00:00",
            "seller_price": "597.17",
            "buyer_price": "597.17",
            "created_at": "2022-03-22T11:37:51.000000Z",
            "updated_at": "2022-03-22T11:37:51.000000Z"
        }
    ],
    "ticket_detail": {
        "id": 112,
        "status": {
            "id": 2,
            "name": "booked",
            "created_at": "2022-03-15T10:23:06.000000Z",
            "updated_at": "2022-03-15T10:23:06.000000Z"
        },
        "user": {
            "id": 11,
            "name": "34567",
            "email": "[email protected]",
            "fiscal_code": "RVNLSS72D17D548R",
            "tel": "98765456",
            "image": "qjg3r0wb2184a2C.png",
            "user_type": "both",
            "status": "otp_not_verified",
            "otp_code": 74671,
            "otp_expiry_time": "2022-03-15 16:08:45",
            "email_verified_at": null,
            "device_token": null,
            "last_login_at": "2022-03-24 19:12:44",
            "created_at": "2022-03-15T10:23:30.000000Z",
            "updated_at": "2022-03-24T13:27:44.000000Z"
        }
    }
}
1

There are 1 best solutions below

0
On

I would probably group by a closure

->groupBy(function ($item) {
   return $item->ticket_detail->status->name ?? 'no-status';
}

If you do this and it returns any with no status then your original query might had worked if all children had status and name