jeudi 19 septembre 2019

Laravel 5.8+get array of columns and rows from query

Relatively new to Laravel and trying to create a REST api that will have as a response a json that is split between rows and cols of a query

So, the code used to generate the json is:

$r = array(
        'cols' => array_keys(get_object_vars(Dd_hr::take(2)->get())),
        'rows' => Dd_hr::take(2)->get()
    );
return response()->json($r, 201);

Basically, the json should look something like:

{"cols":[{"id", "user_name"}],"rows":[{"710206","user"}]}

But in fact it looks like:

{"cols":[],"rows":[{"id":"710206","user_name":"user"}]}

Could someone help me to solve this issue?

Thank you!



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire