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.7] Allow Model destroy method to accept a collection of ids #25878

Merged
merged 5 commits into from
Oct 3, 2018
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
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