mercredi 11 juillet 2018

properly forming a collection into a json response larave 5.6

I have a laravel collection as such:

Collection {#4027 ▼
  #items: array:9 [▼
    6 => HighRisk {#1945 ▶}
    5 => HighRisk {#1944 ▶}
    4 => HighRisk {#1943 ▶}
    3 => HighRisk {#1942 ▶}
    2 => HighRisk {#1941 ▶}
    8 => HighRisk {#1947 ▶}
    1 => HighRisk {#1940 ▶}
    0 => HighRisk {#1939 ▶}
    7 => HighRisk {#1946 ▶}
  ]
}

When I create a response object as such:

     $response = [
        'count' => $count,
        'items' => $items->toArray(),
        'columns' => [
            ['index' => 'part_name', 'name' => 'Part Name'],
            ['index' => 'job_name', 'name' => 'Last Inspection'],
            ['index' => 'inspection_date', 'name' => 'Last Inspection Date'],
            ['index' => 'amber_alert', 'name' => 'Amber Alert'],
            ['index' => 'red_alert', 'name' => 'Red Alert'],
            ['index' => 'min_points', 'name' => 'Minimum Shot'],
        ]
    ];

    return $response()->json($response);

My response object looks like:

enter image description here

As you can see it's like an array of objects (or something?) and I want it to look like: [{}, {}, {}] with out the indexes that you see to the left of the objects. Any ideas?

For reference it should look simmilar to these other response objects:

enter image description here

As you can see the difference between the two images is that one is:

{0: {...}, ... } (top image) and the other (bottom image) is: [{...}, {...}, {...}, ...]



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire