-
Notifications
You must be signed in to change notification settings - Fork 390
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
Added withTrashed to auditable relation and avoid logging item as updated when restoring #222
Conversation
@@ -40,7 +40,16 @@ public function created(AuditableContract $model) | |||
*/ | |||
public function updated(AuditableContract $model) | |||
{ | |||
Auditor::execute($model->setAuditEvent('updated')); | |||
if ( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Expected 0 spaces after opening bracket; newline found
src/AuditableObserver.php
Outdated
$model->isDirty($model->getDeletedAtColumn()) && | ||
!$model->deleted_at && | ||
count($model->getDirty()) == 2 //deleted_at and updated_at | ||
){ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Expected 1 space after closing parenthesis; found 0
src/AuditableObserver.php
Outdated
count($model->getDirty()) == 2 //deleted_at and updated_at | ||
){ | ||
// only restoring softdeleted item, no values changed other than deleted_at an updated_at | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Expected 1 space after closing brace; newline found
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for contributing, but your code needs improvements.
method_exists($model, 'getDeletedAtColumn') && | ||
$model->isDirty($model->getDeletedAtColumn()) && | ||
!$model->deleted_at && | ||
count($model->getDirty()) == 2 //deleted_at and updated_at |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While this may work, I don't like how the code is written. Specially the empty if
part.
@@ -93,7 +93,7 @@ public function getTable() | |||
*/ | |||
public function auditable() | |||
{ | |||
return $this->morphTo(); | |||
return $this->morphTo()->withTrashed(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can do this in your own code $audit->auditable()->withTrashed()
😉
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds like a good idea, thanks!
@gmedeiros, and if you ignore these properties, could it help? <?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use OwenIt\Auditing\Auditable;
use OwenIt\Auditing\Contracts\Auditable as AuditableContract;
class Post extends Model implements AuditableContract;
{
use Auditable;
/**
* Attributes to exclude from the Audit.
*
* @var array
*/
protected $auditExclude = [
'updated_at', 'deleted_at'
];
} |
Thanks for the pull request @gmedeiros. A cleaner implementation has been done in #233. This PR is now closed. |
When retreiving a softdeleted item, I was getting an error, so I added withTrashed when at the auditable() relation. This is because I am auditing a model and some related models using many to many relationships (which required a few changes and not using the sync() method.
Also, when restoring a softdeleted item, an updated audit was being created. I added some logic to check if only the updated_at and deleted_at are changing (with deleted_at being not null) before saving an updated audit.