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

Logs collection does not update upon deleting an object #27

Closed
abodnar opened this issue Feb 2, 2016 · 9 comments
Closed

Logs collection does not update upon deleting an object #27

abodnar opened this issue Feb 2, 2016 · 9 comments

Comments

@abodnar
Copy link

abodnar commented Feb 2, 2016

While working on some code where I'm using this package, I found that if I delete an object the log collection does not update to contain a log entry for the deletion. This may be expected, but I wanted to be sure. I have soft deletes on my object, so maybe that has something to do with it. Though I do know it's logging the deleting in the logs table.

$obj = new Obj();
$obj->title = 'title';
$result = $obj->save();
$this->assertEquals(1, $result, 'Expecting id of 1 after first record created');

$log = $obj->logs()->first();
$this->assertNotEmpty($log, 'Expected a log entry for the creation of the obj');

$obj->title = 'Updated title';
$obj->save();

$logs = $obj->logs;
$this->assertCount(2, $logs, 'Expected two log entries after changing the title of the obj');

obj->delete();
$logs = $obj->logs;

echo count($logs); // = 2

Also I found that I get an error when trying to use logs()->last().

BadMethodCallException: Call to undefined method Illuminate\Database\Query\Builder::last()

@anteriovieira
Copy link
Member

Hi @abodnar ,

Please show me your model.

@abodnar
Copy link
Author

abodnar commented Feb 3, 2016

Here is my model.

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use OwenIt\Auditing\AuditingTrait;

class Obj extends Model
{
    use AuditingTrait;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'title'
    ];

    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = [
    ];

    /**
     * The attributes to exclude in logging for audit logging
     *
     * @var array
     */
    protected $dontKeepLogOf = [
        'created_at',
        'updated_at'
    ];
}

@anteriovieira
Copy link
Member

Hi There,
Are you still having trouble?

I tested here and it's all right

@abodnar
Copy link
Author

abodnar commented Feb 9, 2016

Yes, it is still not returning a log for the deletion of the object.

@anteriovieira
Copy link
Member

@abodnar You are with the latest version?

@abodnar
Copy link
Author

abodnar commented Feb 10, 2016

I am on version 2.1.7

@anteriovieira
Copy link
Member

Hi @abodnar ,

I tried many ways and can not seem simulate the error.

I created this example https://github.com/anteriovieira/laravel-auditing-deletion for you to help me find the error. The application is working, please test.

I await your reply. Thank you.

@abodnar
Copy link
Author

abodnar commented Feb 11, 2016

So I looked at what you did compared to how I was using the logs. The difference I found is that I'm using $obj->logs versus using \OwenIt\Auditing\Log::all()->toArray() to get the logs. When I use your method to get the logs, I definitely have the deletion log. Wouldn't this get all logs for any changed items?

So the problem I see is that the object itself is not having it's logs property updated when a deletion is done.

@anteriovieira
Copy link
Member

Hi @abodnar , It is not possible to obtain the logs of the object excluded, because at that time it no longer exists. For the logs that record you'll have to use another implementation.

Example:

    /* Get user */
    $userCreated = \App\User::all()->last();
    $log = new OwenIt\Auditing\Log();

    /* Delete user */
    $userCreated->delete();

    /* Get logs */
    dd( $log->where('owner_type', App\User::class)->get() );

    // and another implementations ...

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

No branches or pull requests

2 participants