jeudi 19 mai 2016

laravel 5 flatten collection

i have an Illuminate\Database\Eloquent\Collection object resulting from this query:

$incaricoIndex = Incarico::with(['assicurato'])->get();

I have a parent table incarico and a related assicurato one; they are related by incarico.idAssicurato and assicurato.id field.

With my query I get this schema:

{
    "id": 1,
    "amministratore": null,
    "blocco": null,
    "broker": null,
    "capControparte": null,
    "capSinistro": null,
    "controparte": "",
    [CUT]
    "evento": { // related table
        "tipoEvento": "Fuoco"
    }
}

but I would like to have this:

{
    "id": 1,
    "amministratore": null,
    "blocco": null,
    "broker": null,
    "capControparte": null,
    "capSinistro": null,
    "controparte": "",
    [CUT]
    "tipoEvento": "Fuoco"
}

and i need to have all the related data flatted at the same level of the parent object attributes.

Is it necessary to convert it to an array for proper manipulation or is there an helper available?

I will prefer the second case so I could maintain the eloquent object type..

Thank you

Alex



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/1U1AbLs
via IFTTT

Aucun commentaire:

Enregistrer un commentaire