Laravel-auditing: Updated command do not record in audits table

Created on 15 Feb 2017  路  15Comments  路  Source: owen-it/laravel-auditing

Hi, Im using Laravel 5.2 and Auditing 3.1.
When updating a post, only the USERS table writes the UPDATED action. The other tables do not. Below are sample images:

My USER model - UPDATED works fine
captura de tela 2017-02-15 as 18 44 06

My NEWS Model - UPDATED do not record in audits table:
captura de tela 2017-02-15 as 18 46 41

duplicate

Most helpful comment

Hi @rafalau ,

This may be the same as https://github.com/owen-it/laravel-auditing/issues/139#issuecomment-257004656

This type of query does not trigger events in laravel: $model->where(...)->update(...)

You will probably have to find the record, make the changes, and then save.

Or force an audit log.

All 15 comments

Works with ->save();
Dont work with ->update();

Hi @rafalau

Try using the development version:

composer require owen-it/laravel-auditing 4.0.x-dev

And let me know if the issue still persists.

You'll have to do minor changes to your code. Check the latest documentation if you have any doubts.

Hi!
I followed the next steps:
1 - composer require owen-it/laravel-auditing 4.0.x-dev
2 - Add the service provider: OwenIt\Auditing\AuditingServiceProvider::class,
3 - Run: php artisan auditing:install
4 - migrate the data base

5 - Configure my Model:
1

And the issue persists!
Works with ->save();
Dont work with ->update();

See My Update Comand (Dont record comand UPDATE in AUDITS DATABASE ):
captura de tela 2017-02-17 as 18 07 57

Se my Save Comand (Works fine)
3

Maybe this?

https://laravel.com/docs/5.4/eloquent

When issuing a mass update via Eloquent, the saved and updated model events will not be fired for the updated models. This is because the models are never actually retrieved when issuing a mass update.

@rafalau, do the logs show anything at all?

What happens if you listen for the auditing / audited events? Do they fire regardless of the Eloquent event (saved, created, updated) that started the audit process?

Hi @rafalau ,

This may be the same as https://github.com/owen-it/laravel-auditing/issues/139#issuecomment-257004656

This type of query does not trigger events in laravel: $model->where(...)->update(...)

You will probably have to find the record, make the changes, and then save.

Or force an audit log.

Hi, How do I force an audit log?

Hello, @quetzyg Just like I said @anteriovieira, $model->where(...)->update(...) does not trigger events in laravel.

@quetzyg, could you provide an example of how @rafalau can force an update audit?

@rafalau , take a look here, maybe you can help. I can not create a test example now, but I think @quetzyg will be able to help you later.

Okay, thank you so much for help.

As @anteriovieira mentioned, the way you're updating the model won't trigger the updated event, which in turn won't execute an audit.

I would suggest to update your code from:

$this->model->where('id', $id)->update($dadosForm);

to what you have on the second code you show, but with a slight change:

$dados = $this->model->find('id', $id);
$dados->update($dadosForm);

This should work, but it could still be improved. You should think about using model binding on the routes, so you don't have to query around to get the actual model. The model should just be passed to the postEditar() method.

And while the documentation has examples on how to use the Auditor directly, you shouldn't need to if you play by the rules.

Friends, I do not know how to express my gratitude. It's working perfectly. Here's what the final code looks like:
captura de tela 2017-02-22 as 09 47 39

$dados = $this->model->find($id);
$dados->update($dadosForm);

Thanks @rafalau , thank you for your confidence in our package. And I also thank @quetzyg for his beautiful work.

How do I Audit If I have a million records to update?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

AdamKyle picture AdamKyle  路  7Comments

PrafullaKumarSahu picture PrafullaKumarSahu  路  8Comments

bradenkeith picture bradenkeith  路  4Comments

mnlm picture mnlm  路  5Comments

WDC picture WDC  路  5Comments