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

How to hide relation attributes? #26

Closed
robertogallea opened this issue Apr 9, 2019 · 5 comments
Closed

How to hide relation attributes? #26

robertogallea opened this issue Apr 9, 2019 · 5 comments
Assignees
Labels
bug Something isn't working

Comments

@robertogallea
Copy link

Hi, I am not able to hide specific attributes from relations. I have tried the following:

  1. in User set $gdprHidden = ['relation.attribute']

  2. Add Portable trait to Relation and inside it set $gdprHidden = ['attribute']

What am I missing? Thank you.

@sander3 sander3 self-assigned this Apr 9, 2019
@sander3 sander3 added the question Further information is requested label Apr 9, 2019
@sander3
Copy link
Owner

sander3 commented Apr 9, 2019

Hi there,

Number one will not work. Number two does work, but you forgot to implement the PortableContract on the relation.

@sander3 sander3 closed this as completed Apr 9, 2019
@robertogallea
Copy link
Author

Thank you for your reply. However I still have some problems.

I tried also what you told, but I thought it was uncorrect, since I got an exception.

I have the following (simplifying):

class User extends Authenticable implements PortableContract
{
    use Portable;

    protected $gdprWith = ['relation'];

    protected $gdprHidden = ['password', 'remember_token', 'created_at', 'updated_at'];   

    public function relation()
    {
        return $this->hasOne(Relation::class);
    }

    public function getNameAttribute()
    {
        return ucfirst(strtolower($this->person->name)); // <-- HERE LIES THE PROBLEM
    }

   ...
}

and

class Relation extends Model implements PortableContract
{
    use Portable;

    protected $gdprHidden = ['id'];
    
    ...
}

with this setup I get the following exception:

Property [name] does not exist on this collection instance.

where name is an (existing) attribute of Relation used by an accessor in User

It seems that, notwithstanding it is a hasOne relation, your package loads it as a Collection instead of a single object, so the accessor fails.

If a remove Relation from $gdprWith the relation is correctly loaded but no filtering is performed on the attributes. Any ideas why relations are always load as a Collection?

@sander3 sander3 added bug Something isn't working and removed question Further information is requested labels Apr 9, 2019
@sander3 sander3 reopened this Apr 9, 2019
@sander3
Copy link
Owner

sander3 commented Apr 9, 2019

It seems like the Portable trait lacks hasOne and belongsTo support. Unfortunately, I don't have time to fix this properly this week.

Feel free to use the following workaround for the time being. I'll get back to you with a new release.

    /**
     * Load and transform a portable relation.
     *
     * @param  string  $relation
     * @return void
     */
    private function loadPortableRelation(string $relation)
    {
        $items = $this
            ->$relation()
            ->get()
            ->transform(function ($item) {
                return $item->portable();
            });

        if ($items->count() === 1) {
            $items = $items->first();
        }

        $this->attributes[$relation] = $items;
    }

@robertogallea
Copy link
Author

Ok, thank you for your time

@robertogallea
Copy link
Author

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants