How to change key names in laravel collection?

Viewed 68

I have one model instance in which i have many to many relationship data and now everything is in good condition the only thing which i am having trouble is their keys name . As I want to send better camelCase keys json in response but i am not able to change it

my collection object while converting it in array

array:28 [
  "id" => 268897045
  "name" => "remitterNew22June"
  "name_last" => "singh"
  "beneficiary_json" => null
  "flag" => 0
  "consumed_on_paytm" => null
  "consumed_on_fino" => null
  "consumed_on_icici" => null
  "consumed_on_federal" => null
  "consumed_on_airtel" => "0.00"
  "paytm_key" => ""
  "ipaytm_otp_state" => ""
  "paytm_status" => 0
  "prabhumoney_key" => "0"
  "beneficiaries" => array:2 [
    0 => array:28 [
      "id" => 11969779
      "account" => "ATROUNTNUMBEE"
      "name" => "firstName lastName"
      "address" => null
      "last_success_dt" => null
      "created_dt" => null
      "modifide_dt" => "2022-06-17 17:36:26"
      "rl_remitter_id" => null
      "rl_bene_id" => null
      "rl_verification" => 0
      "sm_remitter_id" => null
      "sm_bene_ids" => null
      "sm_verification" => 0
      "user_id" => "295906"
      "verification" => 0
      "remitter_id" => 0
      "india" => 1
      "nepal" => 0
      "mode" => 1
      "paytm_key" => ""
      "bene_id" => "32639604bffe8c8c79361d7fce5e24ca"
      "pivot" => array:2 [
        "remitter_mobile" => "9808908901"
        "bene_id" => "32639604bffe8c8c79361d7fce5e24ca"
      ]
    ]
    1 => array:28 [
      "id" => 11969780
      "account" => "ATROUNTNUMBEE"
      "name" => "firstName lastName"
      "address" => null
      "last_success_dt" => null
      "created_dt" => null
      "modifide_dt" => "2022-06-22 12:47:02"
      "rl_remitter_id" => null
      "rl_bene_id" => null
      "rl_verification" => 0
      "sm_remitter_id" => null
      "sm_bene_ids" => null
      "sm_verification" => 0
      "user_id" => "295906"
      "verification" => 0
      "remitter_id" => 0
      "india" => 1
      "nepal" => 0
      "mode" => 1
      "paytm_key" => ""
      "bene_id" => "179dce7acecd983e64f2a0bd0155f444"
      "pivot" => array:2 [
        "remitter_mobile" => "9808908901"
        "bene_id" => "179dce7acecd983e64f2a0bd0155f444"
      ]
    ]
  ]
]

but this was in collection and converting it in array all the keys are in snake case now my prime focus was to change those keys

lets pick name_last in outer array i wanted to name it as lastName but using collection

as i know that i can do it by using sql alias or query builder but i dont want to ruin my collection . So the point is .. is there any way to handle this issue one method i have found but I am not sure is this right way or not

        $remitterDetails->beneficiaries = $remitterDetails->beneficiaries->map(function ($beneficiary) {
            return [
                'account' => $beneficiary->account,
                'name' => $beneficiary->name,
                'gender' => $beneficiary->gender,
                'beneficiaryId' => $beneficiary->bene_id,
            ];
        });

So in short I have one model instance in which i have one many to many relationship binded data and now i just want to return this data in modified form in which i can modify keys of both array

outer array

inner array of beneficiaries

Thank you !!

0 Answers
Related