Laravel-auditing: Fails to log when updating model

Created on 11 May 2017  路  5Comments  路  Source: owen-it/laravel-auditing

VERSION: I am using v4.0.5
MODEL SET UP: I have my model set up as shown:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use OwenIt\Auditing\Auditable;
use OwenIt\Auditing\Contracts\Auditable as AuditableContract;

class Consignee extends Model implements AuditableContract
{
    use Auditable;
    protected $table = 'consignees';
    protected $primaryKey = 'consignee_id';
    protected $fillable = ['consignee_name'];
    public $timestamps = false;
}

When I create a new record, the audits table is populated correctly, logging the create operation details as it is supposed to. Here I am fine!
PROBLEM:
The issue I am facing is that, when I update the record, the audits table is not updated to show the change.
Where could I possibly be going wrong?

Edited: proper code highlighting (@quetzyg)

duplicate help wanted

Most helpful comment

Thank you @quetzyg for pointing me to the troubleshooting section.
My problem was how I have been handling the updates.
This is what I have been doing:

Consignee::where('consignee_id',$consignee_id)
    ->update(['consignee_name'=>$consignee_name]);

I changed it to this:

Consignee::find($consignee_id)
    ->update(['consignee_name'=>$consignee_name]);

Now apart from the troubleshooting section ,I also found this warning on the Laravel documentation

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.

In my first case, the events were not being fired.
Thank you very much.
Everything works fine and I am getting my logs for all create,update & delete operations.

All 5 comments

Hi there @romoka,

What PHP/Laravel version are you using?
Can you also provide the migrations you're using for the audits and consignees tables, so I can reproduce it locally?

Thank you for replying.
php -v gives me: PHP 7.0.15-0ubuntu0.16.04.4 (cli)
php artisan -V gives Laravel Version: 5.4.22

The migrations:

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateAuditsTable extends Migration
{

    public function up()
    {
        Schema::connection(config('audit.drivers.database.connection'))
            ->create(config('audit.drivers.database.table'), function (Blueprint $table) {
                $table->increments('id');
                $table->unsignedInteger('user_id')->nullable();
                $table->string('event');
                $table->morphs('auditable');
                $table->text('old_values')->nullable();
                $table->text('new_values')->nullable();
                $table->string('url')->nullable();
                $table->ipAddress('ip_address')->nullable();
                $table->string('user_agent')->nullable();
                $table->timestamp('created_at');
            });
    }


    public function down()
    {
        Schema::connection(config('audit.drivers.database.connection'))
            ->drop(config('audit.drivers.database.table'));
    }
}

CONSIGNEE
```php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateConsigneesTable extends Migration
{

public function up()
{
    Schema::create('consignees', function (Blueprint $table) {
        $table->increments('consignee_id');
        $table->string('consignee_name')->unique();         
    });
}


public function down()
{
    Schema::dropIfExists('consignees');
}

}```

How exactly are you updating the model? Show some code.

Also, have you had a look at the troubleshooting section of the documentation?

Thank you @quetzyg for pointing me to the troubleshooting section.
My problem was how I have been handling the updates.
This is what I have been doing:

Consignee::where('consignee_id',$consignee_id)
    ->update(['consignee_name'=>$consignee_name]);

I changed it to this:

Consignee::find($consignee_id)
    ->update(['consignee_name'=>$consignee_name]);

Now apart from the troubleshooting section ,I also found this warning on the Laravel documentation

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.

In my first case, the events were not being fired.
Thank you very much.
Everything works fine and I am getting my logs for all create,update & delete operations.

Glad to hear mate!

Was this page helpful?
0 / 5 - 0 ratings