0
votes

Is there an easy way to not return guarded properties for an eloquent model?

I want to return a User and build a similar User object on the front end. However, I don't want to return the Users password, type_id, remember_token and other properties. Is there a simple way I can restrict the application from returning these properties? I'm aware I can a write a method or attribute that returns only the values I want, but I'm wondering if there's an unguarded() function for eloquent or collections.

If not, how would you recommend doing this. I'd probably scope it as opposed to wrting an attribute, mutator or method.

1

1 Answers

0
votes

You can override the hidden attribute. All fields deifined in this array will not rendered in toArray function.

Your model class:

protected $hidden = ['_token', 'type_id', ...];