Skip to content

Commit

Permalink
Merge pull request #22 from joostbaptist/2.1.4-signature-fix
Browse files Browse the repository at this point in the history
Fix detach method when called without IDs
  • Loading branch information
fico7489 committed Feb 22, 2018
2 parents 2d94f09 + 85c77d5 commit 31e7f4a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/Relations/BelongsToManyCustom.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,12 @@ public function attach($ids, array $attributes = [], $touch = true)
* @param bool $touch
* @return int
*/
public function detach($ids = [], $touch = true)
public function detach($ids = null, $touch = true)
{
if (is_null($ids)) {
$ids = $this->query->pluck($this->query->qualifyColumn($this->relatedKey))->toArray();
}

list($idsOnly) = $this->getIdsWithAttributes($ids);

$this->parent->fireModelEvent('pivotDetaching', true, $this->getRelationName(), $idsOnly);
Expand Down
15 changes: 15 additions & 0 deletions tests/PivotEventTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,21 @@ public function test_detach_collection()
$this->check_variables(0, [1, 2]);
}

public function test_detach_null()
{
$this->startListening();
$user = User::find(1);
$user->roles()->attach([1, 2 ,3]);
$this->assertEquals(3, \DB::table('role_user')->count());

$this->startListening();
$user->roles()->detach();

$this->assertEquals(0, \DB::table('role_user')->count());
$this->check_events(['eloquent.pivotDetaching: ' . User::class, 'eloquent.pivotDetached: ' . User::class]);
$this->check_variables(0, [1, 2, 3]);
}

public function test_update()
{
$this->startListening();
Expand Down

0 comments on commit 31e7f4a

Please sign in to comment.