owen-it / laravel-auditing

Record the change log from models in Laravel
https://laravel-auditing.com
MIT License
3.01k stars 387 forks source link

UUID user_type and user_id store error #918

Closed aytsystems closed 2 months ago

aytsystems commented 6 months ago

I use laravel 9 and I'm using UUID in all tables. I'm having problems registering the user_id and user_type. The other fields are registered normally. I`m using API with Sanctum Auth

I didnt found $table->nullableMorphs('owner') on my User migration.

Audit migration

  public function up()
  {
      $connection = config('audit.drivers.database.connection', config('database.default'));
      $table = config('audit.drivers.database.table', 'audits');

      Schema::connection($connection)->create($table, function (Blueprint $table) {

          $morphPrefix = config('audit.user.morph_prefix', 'user');

          $table->bigIncrements('id');
          $table->string($morphPrefix . '_type')->nullable();
          $table->uuid($morphPrefix . '_id')->nullable();
          $table->string('event');
          $table->string('auditable_type');
          $table->uuid('auditable_id');
          $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();

          $table->index([$morphPrefix . '_id', $morphPrefix . '_type']);
      });
  }
aytsystems commented 6 months ago

In the file OwenIt\Auditing\Auditable; on line 346 and 347 in the
$morphPrefix. '_id' and $morphPrefix . '_type'

I set the variables manually just for testing $morphPrefix . '_id' => auth()->user()->id, $morphPrefix . '_type' => 'App\Models\User', And it worked!

then noticed that the functions $user->getAuthIdentifier() and $user->getMorphClass() it's not working for me.

aldinokemal commented 5 months ago

$user->getAuthIdentifier() $user->getMorphClass() you said that that function is no working.

Just wanna make sure, have you use HasUuids in your model?

parallels999 commented 4 months ago

I set the variables manually just for testing

$morphPrefix . '_id' => auth()->user()->id,
$morphPrefix . '_type' => 'App\Models\User',

And it worked! then noticed that the functions $user->getAuthIdentifier() and $user->getMorphClass() it's not working for me.

https://github.com/owen-it/laravel-auditing/blob/f236e42c6cf8ba6264499e205d59d4c0b37d1def/src/Resolvers/UserResolver.php#L28

User resolver returns auth()->user() Illuminate/Contracts/Auth/Guard.php#L24-L26 user() returns Illuminate/Auth/Authenticatable.php and Illuminate/Auth/Authenticatable.php has getAuthIdentifier