Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ethar's avatar
Level 5

return data using API Resource From collection

i have this collection,

"equipments": [
   {
         "equipment_type": "payment_method",
         "equipment_value": "payment_method_types_pay1"
   },
  {
        "equipment_type": "payment_method",
        "equipment_value": "payment_method_types_pay4"
},
{
       "equipment_type": "ext_int_sunroof",
       "equipment_value": "ext_int_sunroof_pano"
},
{
       "equipment_type": "ext_int_steering",
      "equipment_value": "ext_int_steering_stco"
},

i want to to use API Resource to return data like this

'payament_method' : ["payment_method_types_pay1","payment_method_types_pay4"]
"ext_int_sunroof":"ext_int_sunroof_pano",
"ext_int_steering":"ext_int_steering_stco"

i try use this code

            'payment_method'=>$this->equipments->map(function ($item) {
                    if($item['equipment_type'] == 'payment_method'){
                        return $item->equipment_value;
                    }
                }),

but not work and return null if key not equal

"payment_method": [
     "payment_method_types_pay1",
     "payment_method_types_pay2",
     "payment_method_types_pay4",
     null,
     null,
     null,
]

i want to return data without null, if null return nothing

0 likes
2 replies

Please or to participate in this conversation.