Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Getting rid of numbers in a json response ( LARAVEL 9 )

i’m having some problems with my json response.

When I make an api call from a user (id:1) it returns the response well, but if i change the user (f.e id:2) it returns it with some identifiers on it. Any idea?

Response from id:1

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

{
    "data": [
        {
            "id": 1,
            "matricula": "222555999C",
            "capacidad": "220",
            "created_at": "2022-04-06T09:52:39.000000Z",
            "updated_at": "2022-04-06T09:52:39.000000Z",
            "deleted_at": null,
            "transportista": 1
        }
    ]
}

Response from id:2

{
    "data": {
        "3"(<--- this is the problem): {
            "id": 9,
            "matricula": "HYK 2024",
            "capacidad": "100",
            "created_at": "2022-04-08T15:12:22.000000Z",
            "updated_at": "2022-04-08T15:12:22.000000Z",
            "deleted_at": null,
            "transportista": 2
        }
    }
}

Thanks!

EDIT 1

This is how I am getting the response:

public function getCamiones(Request $request) {
    $id = $request->get('id');
    $camiones = Camiones::all()->where('transportista',$id);
    return response()->json([
        'data' => $camiones,
    ]);
}

>Solution :

Right now, you’re getting all of the results, then filtering the collection returned, instead of having the database do the work for you. Instead, grab only a single row.

$camiones = Camiones::where('transportista', $id)->first();

To get all the rows that match, then you need

$camiones = Camiones::where('transportista', $id)->get();
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading