Laravel-auditing: Audits always coming back as empty collection

Created on 19 Feb 2018  路  4Comments  路  Source: owen-it/laravel-auditing

| Q | A
| ----------------- | ---
| Bug? | yes
| New Feature? | no
| Framework | Laravel
| Framework version | 5.6.*
| Package version | ^6.0
| PHP version | PHP 7.1.10-1+ubuntu16.04.1+deb.sury.org+1

Actual Behaviour

No matter the method I use, I cannot get audits to record. The collection comes back as empty.

Expected Behaviour

I would expect the changes on a model to save

Steps to Reproduce

I have a call log model, implementing Auditable

```

namespace App;

use App\Transformers\CallLogTransformer;
use Illuminate\Database\Eloquent\Model;
use Laravel\Scout\Searchable;
use OwenIt\Auditing\Contracts\Auditable;

class CallLog extends Model implements Auditable
{
use Searchable, \OwenIt\Auditing\Auditable;

protected $fillable = [
    'flow',
    'dealership_id',
    'type',
    'store_advisor_id',
    'caller_information_name',
    'caller_information_dms_id',
    'notes'
];

protected $casts = [
    'type' => 'array'
];

public function results()
{
    return $this->hasMany('App\CallLogResult');
}

public function user()
{
    return $this->belongsTo('App\User');
}

public function toSearchableArray()
{
    return fractal()
           ->item( $this )
           ->transformWith( CallLogTransformer::class )
           ->toArray();
}

}

I run the following in `php artisan tinker`:
1. `factory(\App\CallLog::class)->create()`
2. ` \App\CallLog::first()`and verify the model creates
3. `\App\CallLog::first()->update(['notes'=>'new note']);`
4. ` \App\CallLog::first()`and verify the model updated
5. `\App\CallLog::first()->audits` returns an empty collection, so does `OwenIt\Auditing\Models\Audit::all()`


Here's my composer file:

{
"name": "laravel/laravel",
"description": "The Laravel Framework.",
"keywords": ["framework", "laravel"],
"license": "MIT",
"type": "project",
"require": {
"php": ">=7.0.0",
"algolia/algoliasearch-client-php": "^1.25",
"fideloper/proxy": "~4.0",
"laravel/framework": "5.6.",
"laravel/passport": "^5.0",
"laravel/scout": "^4.0",
"laravel/tinker": "~1.0",
"owen-it/laravel-auditing": "^6.0",
"predis/predis": "^1.1",
"pusher/pusher-php-server": "~3.0",
"spatie/laravel-fractal": "^5.3",
"spatie/laravel-permission": "^2.7"
},
"require-dev": {
"codedungeon/phpunit-result-printer": "^0.6.0",
"filp/whoops": "~2.0",
"fzaninotto/faker": "~1.4",
"laravel/homestead": "^6.5",
"mockery/mockery": "0.9.
",
"phpunit/phpunit": "~7.0"
},
"autoload": {
"classmap": [
"database/seeds",
"database/factories"
],
"psr-4": {
"App\": "app/"
}
},
"autoload-dev": {
"psr-4": {
"Tests\": "tests/"
}
},
"extra": {
"laravel": {
"dont-discover": [
]
}
},
"scripts": {
"post-root-package-install": [
"@php -r \"file_exists('.env') || copy('.env.example', '.env');\""
],
"post-create-project-cmd": [
"@php artisan key:generate"
],
"post-autoload-dump": [
"Illuminate\Foundation\ComposerScripts::postAutoloadDump",
"@php artisan package:discover"
]
}
}
```

I have walked through the install and configuration documentation a number of times and am having a hard time seeing what I could be doing wrong. Any help in tracking down this issue would be much appreciated. Thank you!

duplicate

Most helpful comment

If you're using Tinker, you'll need to enable audits from the CLI. Read the Troubleshooting section.

All 4 comments

If you're using Tinker, you'll need to enable audits from the CLI. Read the Troubleshooting section.

Thanks for the quick response. I resulted to Tinker after I was having issues getting this working in a Test. Future users might benefit from knowing that if they're using Audits within tests, console needs to be set to true as well. Thank you!

While stated in a few areas of the documentation, I'm gonna update the troubleshooting to make sure that when I mean console, I mean anything that runs in a CLI environment (migrations, tests, commands, Tinker, etc).

Also discussed in #381, #380, #351.

Rock on! Thanks for maintaining this package so well.

Was this page helpful?
0 / 5 - 0 ratings