phplaravelloggingeloquentlaravel-auditing

Laravel Audit and UUIDS?


I am using laravel auditing it shows an error on UUIDs I think, I have installed and configure the laravel auditing from http://www.laravel-auditing.com/docs/9.0/installation and change the user_id and id to UUIDs where the default types are bigInteger

  public function up()
{
    Schema::create('audits', function (Blueprint $table) {
        $table->uuid('id')->primary();
        $table->string('user_type')->nullable();
        $table->uuid('user_id')->nullable();
        $table->string('event');
        $table->morphs('auditable');
        $table->text('old_values')->nullable();
        $table->text('new_values')->nullable();
        $table->text('url')->nullable();
        $table->ipAddress('ip_address')->nullable();
        $table->string('user_agent', 1023)->nullable();
        $table->string('tags')->nullable();
        $table->timestamps();
        
    });
}

I have also use the UUIDs Trait in audit.php as i am doing in my other projects model I migrated the project it created a table when i store the data it show the following error

SQLSTATE[22P02]: Invalid text representation: 7 ERROR: invalid input syntax for type bigint: "974afba9-556e-4ecd-bd42-aaf12ce9aab0" (SQL: insert into "audits" ("old_values", "new_values", "event", "auditable_id", "auditable_type", "user_id", "user_type", "url", "ip_address", "user_agent", "tags", "id", "updated_at", "created_at") values ([], {"title":"Magnam nulla perfere","code":"Temporibus in ut cul","created_by":"2d03731c-9c71-4238-bdfc-03e3f556ddef","end_date":"2012-09-29","start_date":"2016-12-02","currency":"PKR","status":"Ongoing","budget":"32","objectives":"Architecto adipisci","specificobjectives":"Eveniet harum repre","abreviation":"Aut iusto quod quia","pcr_deadline":"1976-04-28","donor":"UNICEF","donor_fp_name":"Tanner Noble","donor_fp_email":"gyvimyz@mailinator.com","donor_fp_designation":"Ut vel quidem sed fa","donor_fp_mobile":"Qui sit provident","donor_fp_phone_office":"+1 (832) 163-8003","brsp_fp_email":"wygihihyw@mailinator.com","brsp_fp_name":"Myles Cameron","brsp_fp_designation":"Laboris ut eius ut e","brsp_fp_mobile":"Doloribus quis possi","brsp_fp_phone_office":"+1 (891) 256-8645","reporting_frequency":"Monthly","id":"974afba9-556e-4ecd-bd42-aaf12ce9aab0","updated_at":"2021-07-13 15:32:09","created_at":"2021-07-13 15:32:09"}, created, 974afba9-556e-4ecd-bd42-aaf12ce9aab0, App\Models\Project, 2d03731c-9c71-4238-bdfc-03e3f556ddef, App\Models\User, http://localhost:8000/project/store, 127.0.0.1, Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36, ?, eda36c0b-13c3-42c1-a345-fe2bb4b27520, 2021-07-13 15:32:09, 2021-07-13 15:32:09))


Solution

  • The documentation states you have to do the following changes in the audits table migration when using UUIDs over auto-incremending IDs in your models.

    UUID over auto-incrementing ids

    Some developers prefer to use a UUID instead of auto-incrementing ids. If that's the case, make sure to update the up() method like so:

    For the User, change from

    $table->nullableMorphs('user');
    

    to

    $table->uuid('user_id')->nullable();
    $table->string('user_type')->nullable();
    $table->index([
        'user_id', 
        'user_type',
    ]);
    

    For the Auditable model, change from

    $table->morphs('auditable');
    

    to

    $table->uuid('auditable_id');
    $table->string('auditable_type');
    $table->index([
       'auditable_id', 
       'auditable_type',
    ]);
    

    You already changed the User morphs, but you forgot about the Auditable morphs.