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

[5.3] Fix detach behaviour (BelongsToMany.php) #16144

Closed
wants to merge 5 commits into from
Closed
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
21 changes: 14 additions & 7 deletions src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -1148,9 +1148,10 @@ protected function setTimestampsOnAttach(array $record, $exists = false)
*
* @param mixed $ids
* @param bool $touch
*
* @return int
*/
public function detach($ids = [], $touch = true)
public function detach($ids = null, $touch = true)
{
if ($ids instanceof Model) {
$ids = $ids->getKey();
Expand All @@ -1162,24 +1163,30 @@ public function detach($ids = [], $touch = true)

$query = $this->newPivotQuery();

// If associated IDs were passed to the method we will only delete those
// associations, otherwise all of the association ties will be broken.
// We'll return the numbers of affected rows when we do the deletes.
$ids = (array) $ids;
// If any argument is passed for IDs we will only delete those
// associations, if it's an empty collection or array, code
// will not continue and return zero. If no argument is
// given all of the association ties will be broken.

if (count($ids) > 0) {
$query->whereIn($this->otherKey, $ids);
if ($ids !== null) {
if (count($ids) == 0) {
return 0;
}

$query->whereIn($this->otherKey, (array) $ids);
}

// Once we have all of the conditions set on the statement, we are ready
// to run the delete on the pivot table. Then, if the touch parameter
// is true, we will go ahead and touch all related models to sync.

$results = $query->delete();

if ($touch) {
$this->touchIfTouching();
}

// We'll then return the numbers of affected rows.
return $results;
}

Expand Down