lundi 8 mai 2017

How to build multi array in Transformer Laravel?

I have the following code:

public function transform($obj)
{
    return [
        'id'     => (int) $obj->id,
        'name' => $obj->name,
        "prototype" => $obj->_prototypes()->get()
    ];
}

Where $obj->_prototypes()->get() is collection with fields (id, name).

How to format new array like as:

'name' =>  'name' => $obj->name,
"prototype" => [
    ["id": 1, "name" : "ok"],
     ["id": 2, "name" : "ok 2"],
]

So, I need to iterate $obj->_prototypes()->get() inside transformer object.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire