samedi 19 août 2017

Laravel: Remove an attribute in returned result

I have the following code:

$orders = Order::all();
return $orders;

This returns something like this:

[
     {
         "id": 123,
         "qr_code": "foo.png",
         "qr_code_url": "http://ift.tt/1zQvqaF"
     },
     {
         "id": 112,
         "qr_code": "bar.png",
         "qr_code_url": "http://ift.tt/2x1EX5R"
     }
]

Note that qr_code_url is an appended attribute, and not an attribute stored in the database.

I want to return this collection back to the user without the attribute: qr_code, in this case. So like this:

[
     {
         "id": 123,
         "qr_code_url": "http://ift.tt/1zQvqaF"
     },
     {
         "id": 112,
         "qr_code_url": "http://ift.tt/2x1EX5R"
     }
]

Looking at the collection functions, i can't seem to find an easy way to do this: http://ift.tt/2mY64f6

The only functions I have found close to what I want is: except and forget, but they seem to just work on a 1 dimension array. Not a collection result returned by a model.

How can I solve my problem?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire