| Q | A
| ----------------- | ---
| Bug? | yes
| New Feature? | no
| Framework | Laravel
| Framework version | 5.4.27
| Package version | 4.0.7
| PHP version | 7.0.18
When getting the Audits of a soft-deleted Model, its new_values key comes back empty.
Since a soft-deleted method is actually edited and not really removed (ie. its row stays in the database and its deleted_at field is edited), I expect the audit's new values to reflect that, ie. to show the new value of the deleted_at field.
Create an auditable soft-deletable model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use OwenIt\Auditing\Auditable;
use Illuminate\Database\Eloquent\SoftDeletes;
use OwenIt\Auditing\Contracts\Auditable as AuditableContract;
class SomeModel extends Model implements AuditableContract
{
use SoftDeletes;
use Auditable;
protected $dates = ['deleted_at'];
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'field1', 'field2', 'field3',
];
}
Write a simple test which soft-deletes the model and checks the new values:
<?php
public function testAudits()
{
$modelFields = [
'field1' => 'Field '.rand(),
'field2' => 'mail'.rand().'@mail.com',
'field3' => 'password'.rand(),
];
$model = new SomeModel($modelFields);
// set a proper ID in here
Auth::onceUsingId(User::first()->id);
$model->save();
// soft-delete model
$model->delete();
$model = SomeModel::withTrashed()->with('audits.user')->find($model->getKey());
// get delete audit (the last one) and test
$deleteAudit = $model->audits->last()->toArray();
$this->assertArrayHasKey('deleted_at', $deleteAudit['new_values']);
}
I got it working by adding this block to the Auditable::auditDeletedAttributes() method:
foreach ($this->getDirty() as $attribute => $value) {
if ($this->isAttributeAuditable($attribute)) {
$new[$attribute] = array_get($this->attributes, $attribute);
}
}
(which I copy/pasted from auditUpdatedAttributes())
(ps: don't let the fact that I'm only reporting bugs make you think otherwise, your auditing package is great! Obrigado :-) )
Hi @daniel-gomes-sociomantic,
In this particular case, I wouldn't call it a bug, but more of a feature :-)
The only change - as you very well point out - is the deleted_at, which goes from a NULL to a DATETIME value (and the other way around when you restore).
By default, any kind of timestamp (created_at, updated_at and deleted_at) isn't included in the created audit, so you'll have to take that into account.
To solve your issue, you'll have to set the $auditTimestamps attribute of the Auditable model to true.
Feel free to reopen if you're still having issues with this.
Btw, you could just use the created_at value of the Audit model, since it will have the exact same date/time as the deleted_at key in the new_values column.
h, I see.
It all makes sense, hadn't noticed that $auditTimestamps parameter. All good then!
i have updated the settings globally at the config/audit.php file and set the 'timestamps' => false to true as stated here. thank you very much!