Similar to the possibility which exists with Eloquent models, I think it should be possible to hide some attributes for serialization purposes. For example, I currently serialize models for logging purposes and don't really want userPassword attribute to be included.
Is this possible?
Hi @mgiritli,
You’re right — this feature isn’t built in right now, you’ll have to do it manually. Though it should definitely be included.
Give me a day or two and I’ll have this patched in, thanks for letting me know!
🎉 All done. You can now add $visible and $hidden properties to your models, or use addHidden() / makeHidden() methods on a model instance:
Via Properties:
class User extends Model
{
protected $hidden = ['userPassword'];
}
Via Methods:
$user = User::findByAnr('steve');
$user->addHidden('userPassword');
// Or:
$user->makeHidden('userPassword');
// 'userPassword' will be removed from the encoded array.
json_encode($user);
A new release will be out in a couple hours :smile:
This has now been released in v1.4.0.
Thanks so much, Steve :-)
Glad to help @mgiritli! Please feel free to drop in anytime you have any suggestions :smile: