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

Soft deleted Eloquent models - attribute accessors and modifiers are not applied #450

Closed
milen-vm opened this issue Oct 12, 2018 · 3 comments
Assignees

Comments

@milen-vm
Copy link

Q A
Bug? yes
New Feature? no
Framework Laravel
Framework version 5.5.44
Package version 8.0
PHP version 7.2.10

Actual Behaviour

When Eloquent model is soft deleted on audits listing, attribute accessors and modifiers are not applied. $this->auditable is null in Audit trait.

Expected Behaviour

To be applied attributes accessors and modifiers.

Steps to Reproduce

Eloquent model attribute accessor:
public function getPrivateDataAttribute($value) { return Crypt::decrypt($value); }

or using Audit attribute modifier

protected $attributeModifiers = [ 'private_data' => CryptingEncoder::class, ];

`class CryptingEncoder implements AttributeEncoder
{

public static function encode($value)
{
    return Crypt::encrypt($value);
}

public static function decode($value)
{
    return Crypt::decrypt($value);
}

}`

Possible Solutions

Change relationship in Audit trait (Audit.php)

public function auditable(): MorphTo { return $this->morphTo()->withTrashed(); }

@quetzyg
Copy link
Contributor

quetzyg commented Oct 14, 2018

Hi @milen-vm,

When Eloquent model is soft deleted on audits listing, attribute accessors and modifiers are not applied. $this->auditable is null in Audit trait.

Yup, this is expected. Like you mentioned, when fetching the related Auditable model, add the withTrashed() scope so that you don't get errors with accessors/modifiers.

Not everyone uses the SoftDeletes trait, so that's why it's not there in the first place.

Similar issue already discussed in #278

@quetzyg
Copy link
Contributor

quetzyg commented Oct 14, 2018

Meanwhile, I did a PR which moves the relationship code from the Audit trait to the model. This should make it easier to just implement your own Audit model and set the Auditable and User relationships as you see fit.

The documentation will be updated shortly.

@milen-vm
Copy link
Author

milen-vm commented Oct 16, 2018

Thank you! I hope it will be available soon on packagist.org.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants