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

subject_id is null when deleting a pivot model #1340

Open
RMK147 opened this issue Oct 9, 2024 · 0 comments
Open

subject_id is null when deleting a pivot model #1340

RMK147 opened this issue Oct 9, 2024 · 0 comments
Labels

Comments

@RMK147
Copy link

RMK147 commented Oct 9, 2024

Describe the bug
I'm using the spatie/laravel-permission package and to track the changes I have created the below pivot model:

<?php

namespace Modules\Auth\Models;

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

final class RoleUser extends MorphPivot
{
    use LogsActivity;

    public $incrementing = true;

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

And I have the following on my user model:

use LogsActivity, HasRoles {roles as protected spatRoles;}

public function roles(): BelongsToMany
{
    return $this->spatRoles()->using(RoleUser::class);
}

The subject_id is populated when a role is assigned to a user, now that I added $incrementing = true, but it is still null when a role is removed from the user.

To Reproduce
Things needed to reproduce the error.

Assign roles to a user, and the subject_id is populated as expected. Remove roles from a user, and the subject_id is null. For example:

id	log_name	description	subject_type	event	subject_id	causer_type	causer_id	properties	batch_uuid	created_at	updated_at
50	default	created	Modules\Auth\Models\RoleUser	created	22	Modules\Auth\Models\User	1	{"attributes": {"id": 22, "role_id": 6, "model_id": 7, "model_type": "Modules\\Auth\\Models\\User"}}	7e08874b-cef0-453d-86f3-e86cbe4a13a2	2024-10-09 16:05:44	2024-10-09 16:05:44
51	default	deleted	Modules\Auth\Models\RoleUser	deleted	NULL	Modules\Auth\Models\User	1	{"old": {"role_id": "6", "model_id": 7, "model_type": "Modules\\Auth\\Models\\User"}}	298f4384-5665-4dd0-969a-f8a0f43e157d	2024-10-09 16:06:07	2024-10-09 16:06:07

Expected behavior
The subject_id should not be null when removing a role from a user.

Versions (please complete the following information)

  • PHP: 8.3
  • Database: MySQL 8.3.0
  • Laravel: 11.26.0
  • Package: 4.8.0
@RMK147 RMK147 added the bug label Oct 9, 2024
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

1 participant