Skip to content

Commit eee4057

Browse files
authored
Merge pull request #220 from plank/fix-collection-delete
Fix MediableCollection::delete() bugs
2 parents 73b8eaf + d9cbc41 commit eee4057

File tree

2 files changed

+52
-16
lines changed

2 files changed

+52
-16
lines changed

src/MediableCollection.php

+25-16
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
namespace Plank\Mediable;
55

66
use Closure;
7-
use Illuminate\Database\Eloquent\Builder;
7+
use Illuminate\Database\Query\Builder;
88
use Illuminate\Database\Eloquent\Collection;
99
use Illuminate\Database\Eloquent\Model;
1010
use Illuminate\Database\Eloquent\Relations\MorphToMany;
@@ -116,24 +116,33 @@ public function delete(): void
116116
$query = $relation->newPivotStatement();
117117
$classes = [];
118118

119-
$this->each(function (Model $item) use ($query, $relation, $classes) {
120-
// collect list of ids of each class in case not all
121-
// items belong to the same class
122-
$classes[get_class($item)][] = $item->getKey();
119+
$this->each(
120+
function (Model $item) use ($query, $relation, &$classes) {
121+
// collect list of ids of each class in case not all
122+
// items belong to the same class
123+
$classes[get_class($item)][] = $item->getKey();
124+
}
125+
);
123126

124-
// select pivots matching each item for deletion
125-
$query->orWhere(function (Builder $q) use ($item, $relation) {
126-
$q->where($relation->getMorphType(), get_class($item));
127-
$q->where($relation->getQualifiedForeignPivotKeyName(), $item->getKey());
128-
});
129-
});
127+
// delete each item by class
128+
collect($classes)->each(
129+
function (array $ids, string $class) use ($query, $relation) {
130+
// select pivots matching each item for deletion
131+
$query->orWhere(
132+
function (Builder $q) use ($class, $ids, $relation) {
133+
$q->where($relation->getMorphType(), $class);
134+
$q->whereIn(
135+
$relation->getQualifiedForeignPivotKeyName(),
136+
$ids
137+
);
138+
}
139+
);
140+
141+
$class::whereIn((new $class)->getKeyName(), $ids)->delete();
142+
}
143+
);
130144

131145
// delete pivots
132146
$query->delete();
133-
134-
// delete each item by class
135-
collect($classes)->each(function (array $ids, string $class) {
136-
$class::whereIn((new $class)->getKeyName(), $ids)->delete();
137-
});
138147
}
139148
}

tests/Integration/MediableCollectionTest.php

+27
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Plank\Mediable\Tests\Integration;
44

5+
use Illuminate\Support\Facades\DB;
56
use Plank\Mediable\Media;
67
use Plank\Mediable\MediableCollection;
78
use Plank\Mediable\Tests\Mocks\SampleMediable;
@@ -146,4 +147,30 @@ public function test_it_can_lazy_eager_load_media_with_relations_by_tag_match_al
146147
$this->assertTrue($collection[0]->media[0]->relationLoaded('originalMedia'));
147148
$this->assertTrue($collection[0]->media[0]->relationLoaded('variants'));
148149
}
150+
151+
public function testDelete()
152+
{
153+
$mediable1 = factory(SampleMediable::class)->create(['id' => 1]);
154+
$mediable2 = factory(SampleMediable::class)->create(['id' => 2]);
155+
$mediable3 = factory(SampleMediable::class)->create(['id' => 3]);
156+
$media1 = factory(Media::class)->create(['id' => 1]);
157+
$media2 = factory(Media::class)->create(['id' => 2]);
158+
$mediable1->attachMedia($media1, 'foo');
159+
$mediable1->attachMedia($media2, ['foo', 'bar', 'baz']);
160+
$mediable2->attachMedia($media2, ['foo']);
161+
$mediable3->attachMedia($media2, ['foo']);
162+
$collection = new MediableCollection([$mediable1, $mediable2]);
163+
164+
$collection->delete();
165+
166+
$mediableResults = SampleMediable::all();
167+
$this->assertEquals([3], $mediableResults->modelKeys());
168+
169+
$query = $mediable1->media()->newPivotStatement();
170+
$pivots = $query->get();
171+
$this->assertCount(1, $pivots);
172+
$this->assertEquals("2", $pivots[0]->media_id);
173+
$this->assertEquals("3", $pivots[0]->mediable_id);
174+
$this->assertEquals("foo", $pivots[0]->tag);
175+
}
149176
}

0 commit comments

Comments
 (0)