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.2] Allow the detach method to accept a collection #14412

Merged
merged 1 commit into from
Jul 21, 2016
Merged
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
10 changes: 7 additions & 3 deletions src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -1061,14 +1061,18 @@ protected function setTimestampsOnAttach(array $record, $exists = false)
/**
* Detach models from the relationship.
*
* @param int|array $ids
* @param mixed $ids
* @param bool $touch
* @return int
*/
public function detach($ids = [], $touch = true)
{
if ($ids instanceof Model) {
$ids = (array) $ids->getKey();
$ids = $ids->getKey();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this no longer an array?

}

if ($ids instanceof Collection) {
$ids = $ids->modelKeys();
}

$query = $this->newPivotQuery();
Expand All @@ -1079,7 +1083,7 @@ public function detach($ids = [], $touch = true)
$ids = (array) $ids;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Casting seemed to already be in place here?


if (count($ids) > 0) {
$query->whereIn($this->otherKey, (array) $ids);
$query->whereIn($this->otherKey, $ids);
}

// Once we have all of the conditions set on the statement, we are ready
Expand Down
21 changes: 21 additions & 0 deletions tests/Database/DatabaseEloquentBelongsToManyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,27 @@ public function testDetachWithSingleIDRemovesPivotTableRecord()
$this->assertTrue($relation->detach([1]));
}

public function testDetachMethodConvertsCollectionToArrayOfKeys()
{
$relation = $this->getMock('Illuminate\Database\Eloquent\Relations\BelongsToMany', ['touchIfTouching'], $this->getRelationArguments());
$query = m::mock('stdClass');
$query->shouldReceive('from')->once()->with('user_role')->andReturn($query);
$query->shouldReceive('where')->once()->with('user_id', 1)->andReturn($query);
$query->shouldReceive('whereIn')->once()->with('role_id', [1, 2, 3]);
$query->shouldReceive('delete')->once()->andReturn(true);
$relation->getQuery()->shouldReceive('getQuery')->andReturn($mockQueryBuilder = m::mock('StdClass'));
$mockQueryBuilder->shouldReceive('newQuery')->once()->andReturn($query);
$relation->expects($this->once())->method('touchIfTouching');

$collection = new Collection([
m::mock(['getKey' => 1]),
m::mock(['getKey' => 2]),
m::mock(['getKey' => 3]),
]);

$this->assertTrue($relation->detach($collection));
}

public function testDetachMethodClearsAllPivotRecordsWhenNoIDsAreGiven()
{
$relation = $this->getMock('Illuminate\Database\Eloquent\Relations\BelongsToMany', ['touchIfTouching'], $this->getRelationArguments());
Expand Down