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

[12.x] Fix pivot model observers not working properly when using withPivotValue #55247

Open
wants to merge 2 commits into
base: 12.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,14 @@ public function withPivotValue($column, $value = null)

$this->pivotValues[] = compact('column', 'value');

return $this->wherePivot($column, '=', $value);
// Add where clause for filtering but track it separately so it doesn't
// prevent the pivot model observers from working
$this->wherePivot($column, '=', $value);

// Mark this where clause as coming from withPivotValue
$this->pivotWheres[count($this->pivotWheres) - 1]['from_pivot_value'] = true;

return $this;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ protected function attachNew(array $records, array $current, $touch = true)
public function updateExistingPivot($id, array $attributes, $touch = true)
{
if ($this->using &&
empty($this->pivotWheres) &&
empty($this->getNonPivotValueWhereClauses()) &&
empty($this->pivotWhereIns) &&
empty($this->pivotWhereNulls)) {
return $this->updateExistingPivotUsingCustomClass($id, $attributes, $touch);
Expand Down Expand Up @@ -437,7 +437,7 @@ public function detach($ids = null, $touch = true)
{
if ($this->using &&
! empty($ids) &&
empty($this->pivotWheres) &&
empty($this->getNonPivotValueWhereClauses()) &&
empty($this->pivotWhereIns) &&
empty($this->pivotWhereNulls)) {
$results = $this->detachUsingCustomClass($ids);
Expand Down Expand Up @@ -570,7 +570,15 @@ public function newPivotQuery()
$query = $this->newPivotStatement();

foreach ($this->pivotWheres as $arguments) {
$query->where(...$arguments);
// Clone the arguments to avoid modifying the original array
$argumentsToApply = $arguments;

// Remove the from_pivot_value flag before applying to the query
if (isset($argumentsToApply['from_pivot_value'])) {
unset($argumentsToApply['from_pivot_value']);
}

$query->where(...$argumentsToApply);
}

foreach ($this->pivotWhereIns as $arguments) {
Expand Down Expand Up @@ -691,4 +699,18 @@ protected function getTypeSwapValue($type, $value)
default => $value,
};
}

/**
* Get the pivot where clauses that are not from withPivotValue.
*
* @return array
*/
protected function getNonPivotValueWhereClauses()
{
return collect($this->pivotWheres)
->filter(function ($where) {
return empty($where['from_pivot_value'] ?? false);
})
->all();
}
}
25 changes: 25 additions & 0 deletions tests/Integration/Database/EloquentPivotEventsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,31 @@ public function testCustomMorphPivotClassDetachAttributes()
$project->equipments()->save($equipment);
$equipment->projects()->sync([]);
}

public function testPivotWithPivotValueTriggerEvents()
{
$user = PivotEventsTestUser::forceCreate(['email' => 'taylor@laravel.com']);
$project = PivotEventsTestProject::forceCreate(['name' => 'Test Project']);

// Attach with a specific role
$project->collaborators()->attach($user, ['role' => 'admin']);
PivotEventsTestCollaborator::$eventsCalled = [];

// Get the relation with withPivotValue
$adminRelation = $project->belongsToMany(
PivotEventsTestUser::class, 'project_users', 'project_id', 'user_id'
)->using(PivotEventsTestCollaborator::class)
->withPivotValue('role', 'admin');

// Update through the relation with withPivotValue
$adminRelation->updateExistingPivot($user->id, ['permissions' => ['manage']]);
$this->assertEquals(['saving', 'updating', 'updated', 'saved'], PivotEventsTestCollaborator::$eventsCalled, 'Observer events should fire on updateExistingPivot with withPivotValue');

// Detach through the relation with withPivotValue
PivotEventsTestCollaborator::$eventsCalled = [];
$adminRelation->detach($user->id);
$this->assertEquals(['deleting', 'deleted'], PivotEventsTestCollaborator::$eventsCalled, 'Observer events should fire on detach with withPivotValue');
}
}

class PivotEventsTestUser extends Model
Expand Down
Loading