Skip to content

Commit

Permalink
[5.7] Allow Model destroy method to accept a collection of ids (#25878)
Browse files Browse the repository at this point in the history
* Allow Model destroy method to accept a collection of ids

* add destroy with collection test

* coding style

* refactor nested ternary

* import Collection as BaseCollection
  • Loading branch information
tristanward authored and taylorotwell committed Oct 3, 2018
1 parent 273a541 commit be4cc3b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/Illuminate/Database/Eloquent/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Illuminate\Contracts\Queue\QueueableEntity;
use Illuminate\Database\Eloquent\Relations\Pivot;
use Illuminate\Contracts\Queue\QueueableCollection;
use Illuminate\Support\Collection as BaseCollection;
use Illuminate\Database\Query\Builder as QueryBuilder;
use Illuminate\Database\ConnectionResolverInterface as Resolver;

Expand Down Expand Up @@ -825,7 +826,7 @@ protected function insertAndSetId(Builder $query, $attributes)
/**
* Destroy the models for the given IDs.
*
* @param array|int $ids
* @param \Illuminate\Support\Collection|array|int $ids
* @return int
*/
public static function destroy($ids)
Expand All @@ -835,6 +836,10 @@ public static function destroy($ids)
// type value or get this total count of records deleted for logging, etc.
$count = 0;

if ($ids instanceof BaseCollection) {
$ids = $ids->all();
}

$ids = is_array($ids) ? $ids : func_get_args();

// We will actually pull the models from the database table and call delete on
Expand Down
5 changes: 5 additions & 0 deletions tests/Database/DatabaseEloquentModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,11 @@ public function testDestroyMethodCallsQueryBuilderCorrectly()
$result = EloquentModelDestroyStub::destroy(1, 2, 3);
}

public function testDestroyMethodCallsQueryBuilderCorrectlyWithCollection()
{
$result = EloquentModelDestroyStub::destroy(new \Illuminate\Database\Eloquent\Collection([1, 2, 3]));
}

public function testWithMethodCallsQueryBuilderCorrectly()
{
$result = EloquentModelWithStub::with('foo', 'bar');
Expand Down

0 comments on commit be4cc3b

Please sign in to comment.