Skip to content

Commit f1b17a2

Browse files
authored
Add support to Eloquent Collection on Model::destroy() (#36497)
1 parent f4c0819 commit f1b17a2

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

src/Illuminate/Database/Eloquent/Model.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Illuminate\Contracts\Support\Arrayable;
1111
use Illuminate\Contracts\Support\Jsonable;
1212
use Illuminate\Database\ConnectionResolverInterface as Resolver;
13+
use Illuminate\Database\Eloquent\Collection as EloquentCollection;
1314
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
1415
use Illuminate\Database\Eloquent\Relations\Concerns\AsPivot;
1516
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
@@ -1061,6 +1062,10 @@ protected function insertAndSetId(Builder $query, $attributes)
10611062
*/
10621063
public static function destroy($ids)
10631064
{
1065+
if ($ids instanceof EloquentCollection) {
1066+
$ids = $ids->modelKeys();
1067+
}
1068+
10641069
if ($ids instanceof BaseCollection) {
10651070
$ids = $ids->all();
10661071
}

tests/Database/DatabaseEloquentModelTest.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,16 @@ public function testDestroyMethodCallsQueryBuilderCorrectly()
290290

291291
public function testDestroyMethodCallsQueryBuilderCorrectlyWithCollection()
292292
{
293-
EloquentModelDestroyStub::destroy(new Collection([1, 2, 3]));
293+
EloquentModelDestroyStub::destroy(new BaseCollection([1, 2, 3]));
294+
}
295+
296+
public function testDestroyMethodCallsQueryBuilderCorrectlyWithEloquentCollection()
297+
{
298+
EloquentModelDestroyStub::destroy(new Collection([
299+
new EloquentModelDestroyStub(['id' => 1]),
300+
new EloquentModelDestroyStub(['id' => 2]),
301+
new EloquentModelDestroyStub(['id' => 3]),
302+
]));
294303
}
295304

296305
public function testDestroyMethodCallsQueryBuilderCorrectlyWithMultipleArgs()
@@ -2383,6 +2392,10 @@ public function newQuery()
23832392

23842393
class EloquentModelDestroyStub extends Model
23852394
{
2395+
protected $fillable = [
2396+
'id',
2397+
];
2398+
23862399
public function newQuery()
23872400
{
23882401
$mock = m::mock(Builder::class);

0 commit comments

Comments
 (0)