spatie / laravel-activitylog

Log activity inside your Laravel app
https://docs.spatie.be/laravel-activitylog
MIT License
5.36k stars 714 forks source link

Pivot table changes are not relevant #1081

Closed Christian-Vasilev closed 1 year ago

Christian-Vasilev commented 2 years ago

Describe the bug When I make a change to the User model and assign a role (Many to Many relation). The changes that are added to the attributes and old array keys of the changes made are the same.

To Reproduce I have a basic many to many relation between the User Model and the Role Model. The user model looks like this:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;
use Spatie\Activitylog\LogOptions;
use Spatie\Activitylog\Traits\LogsActivity;

**User Model:** 
class User extends Authenticatable
{
    use HasApiTokens;
    use HasFactory;
    use Notifiable;
    use SoftDeletes;
    use LogsActivity;

    /**
     * The attributes that are mass assignable.
     *
     * @var string[]
     */
    protected $fillable = [
        'name',
        'email',
        'active',
        'password',
    ];

    protected $dates = ['last_login', 'deleted_at'];

    /**
     * The attributes that should be hidden for serialization.
     *
     * @var array
     */
    protected $hidden = [
        'password',
        'remember_token',
    ];

    /**
     * The attributes that should be cast.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
        'created_at' => 'datetime: Y-m-d',
    ];

    /**
     * The relations that should be loaded
     *
     * @var array
     */
    protected $with = ['roles'];

    public function roles()
    {
        return $this->belongsToMany(Role::class)->withTimestamps();
    }

    public function getActivitylogOptions(): LogOptions
    {
        return LogOptions::defaults()
            ->logOnly(['roles'])
            ->logFillable()
            ->logOnlyDirty();
    }
}

Pivot Model:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Relations\Pivot;
use Spatie\Activitylog\LogOptions;
use Spatie\Activitylog\Traits\LogsActivity;

/**
 *
 */
final class RoleUser extends Pivot
{
    use LogsActivity;

    public $incrementing = true;

    public function getActivitylogOptions(): LogOptions
    {
        return LogOptions::defaults()
            ->logFillable()
            ->logOnlyDirty();
    }
}

Pivot model migration:

<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('role_user', function (Blueprint $table) {
            $table->id();
            $table->integer('user_id')->unsigned();
            $table->integer('role_id')->unsigned();
            $table->timestamps();

            $table->primary(['id']);
            $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
            $table->foreign('role_id')->references('id')->on('roles')->onDelete('cascade');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('role_user');
    }
};

This is the part of my controller that updates the releation

 $updated = $user->update([
            'name' => $request->name,
            'email' => $request->email,
            'active' => $request->has('active'),
        ]);

        $user->roles()->sync($request->input('roles'));

Expected behavior When using $activity->changes to get the actual changes that where made.

Screenshots If applicable, add screenshots to help explain your problem.

Versions (please complete the following information)

Additional context none

Exception No exception

Stack Trace No stack

spatie-bot commented 1 year ago

Dear contributor,

because this issue seems to be inactive for quite some time now, I've automatically closed it. If you feel this issue deserves some attention from my human colleagues feel free to reopen it.

moedayraki commented 1 year ago

Is this thing fixed or not yet?

BrayanCaro commented 1 year ago

I think this is very useful, is anyone working on this issue?