Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make it possible to hide some attributes for serialization #112

Closed
mgiritli opened this issue Apr 11, 2020 · 5 comments
Closed

Make it possible to hide some attributes for serialization #112

mgiritli opened this issue Apr 11, 2020 · 5 comments
Labels
enhancement New feature or request

Comments

@mgiritli
Copy link

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?

@stevebauman stevebauman added the enhancement New feature or request label Apr 11, 2020
@stevebauman
Copy link
Member

stevebauman commented Apr 11, 2020

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!

stevebauman added a commit to DirectoryTree/LdapRecord that referenced this issue Apr 12, 2020
@stevebauman
Copy link
Member

stevebauman commented Apr 12, 2020

🎉 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 😄

@stevebauman
Copy link
Member

This has now been released in v1.4.0.

@mgiritli
Copy link
Author

Thanks so much, Steve :-)

@stevebauman
Copy link
Member

Glad to help @mgiritli! Please feel free to drop in anytime you have any suggestions 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants