Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Many to many relations have the same value in old and attributes #1325

Open
peirix opened this issue Sep 11, 2024 · 3 comments
Open

Many to many relations have the same value in old and attributes #1325

peirix opened this issue Sep 11, 2024 · 3 comments
Labels

Comments

@peirix
Copy link

peirix commented Sep 11, 2024

Describe the bug
Basically the same as #1081 but that got closed, so thought I'd create a new one.

We have an Item class that has a many-to-many relationship to Color through the colors():

public function colors()
{
    return $this->belongsToMany(Color::class, 'item_color');
}

And then our getActivitylogOptions is set up as following:

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

The first thing is that I can't tell it to log ['colors.id'] which I would like to, I then get an error saying id attribute does not exist on collection. But also the old colors array and the attribute colors array are always the same. And I found that if I sync the colors first the arrays will have the new value, and if I save the item before syncing the colors it will have the old value

$item->save(); // in this log the arrays are both the old colors
$item->colors()->sync($request->input('colors'));
$item->save(); //  in this log the arrays are both the new colors

To Reproduce

Schema::create('items', function (Blueprint $table) {
    $table->id();
    $table->string('name')->nullable();
    $table->softDeletes();
    $table->timestamps();
});
Schema::create('colors', function (Blueprint $table) {
    $table->id();
    $table->string('name');
    $table->string('slug');
    $table->string('hex_code');
    $table->timestamps();
});
Schema::create('item_color', function (Blueprint $table) {
    $table->foreignIdFor(Item::class)->constrained('items')->onUpdate('cascade')->onDelete('cascade');
    $table->foreignIdFor(Color::class)->constrained('colors')->onUpdate('cascade')->onDelete('cascade');

    $table->primary(['item_id', 'color_id']);
});

Expected behavior
First off I would expect the arrays to be different, but I would also like to be able to specify what attribute of the related model I would like to be in the array

Versions (please complete the following information)

  • PHP: 8.3
  • Database: mysql8
  • Laravel: 11.19.0
  • Package: 4.8.0
@peirix peirix added the bug label Sep 11, 2024
@Jacobs63
Copy link

I believe this library is not suited for logging relations due to the nature of the issue.

To log everything properly, you should not only log the colors of an item changing, but also the items of a color changing, meaning inverse relations should also be logged to have a fully working log.

After finding this out, we've decided to ditch this package and instead create a custom logging solution for this.

@peirix
Copy link
Author

peirix commented Oct 3, 2024

I think that would be overkill, at least for our use case. I don't want it to log anything if the colors themselves change, I just want to log if an item has changed its colors.

So if an item is set to be blue and black first, and then brown gets added as a color later on, I'd like that to be in the activity log. But since it's a many-to-many relationship the colors attribute isn't set directly on the item and it won't pick it up.

Right now I'm handling it with a batch log, so I manually log any changes to the colors as a separate log, and then when I'm viewing the logs I merge them in the view. It works but feels like an unnecessary workaround.

@Jacobs63
Copy link

Jacobs63 commented Oct 7, 2024

It's not an unnecessary workaround. The package simply doesn't support such relations.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants