jeudi 9 février 2017

Get certain attributes from an Eloquent model when converting to an Array in Laravel

I have a model (e.g. User).

I can easily convert it to an array like so:

$user->toArray()

However, this gives all the attributes. I only want attributes x, y and z.

We can use the hidden and visible attributes of the model to hide/show values, as described here: http://ift.tt/2lpnNN8

However, I don't want to use this because this is more of a one off situation. Not a regular occurrence.

The pluck method is ideal, but this only applies to Collections, not Models.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire