Skip to content

Commit 7cbefff

Browse files
committed
Improve untag all query count and add phpdoc for properties
1 parent e0f68b3 commit 7cbefff

File tree

3 files changed

+19
-5
lines changed

3 files changed

+19
-5
lines changed

Diff for: src/IlluminateTag.php

+4
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@
2525
use Illuminate\Database\Eloquent\Relations\HasMany;
2626
use Illuminate\Database\Eloquent\Relations\MorphTo;
2727

28+
/** @property string $name */
29+
/** @property string $namespace */
30+
/** @property string $slug */
31+
/** @property int $count */
2832
class IlluminateTag extends Model
2933
{
3034
/**

Diff for: src/TaggableTrait.php

+14-4
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,12 @@
2020

2121
namespace Cartalyst\Tags;
2222

23+
use Illuminate\Database\Eloquent\Collection;
2324
use Illuminate\Database\Eloquent\Model;
2425
use Illuminate\Database\Eloquent\Builder;
2526
use Illuminate\Database\Eloquent\Relations\MorphToMany;
2627

28+
/** @property Collection $tags */
2729
trait TaggableTrait
2830
{
2931
/**
@@ -172,10 +174,18 @@ public function tag($tags): bool
172174
*/
173175
public function untag($tags = null): bool
174176
{
175-
$tags = $tags ?: $this->tags->pluck('name')->all();
176-
177-
foreach ($this->prepareTags($tags) as $tag) {
178-
$this->removeTag($tag);
177+
if (empty($tags)) {
178+
if ($this->tags()->detach()) {
179+
foreach ($this->tags as $tag) {
180+
/** @var IlluminateTag $tag */
181+
$tag->update(['count' => $tag->count - 1]);
182+
}
183+
$this->tags = new Collection();
184+
}
185+
} else {
186+
foreach ($this->prepareTags($tags) as $tag) {
187+
$this->removeTag($tag);
188+
}
179189
}
180190

181191
return true;

Diff for: tests/TaggableTraitTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public function it_can_remove_all_tags()
8888
$queryCount = $this->withQueryCount(fn () => $post->untag());
8989

9090
$this->assertCount(0, $post->tags);
91-
$this->assertLessThanOrEqual(9, $queryCount);
91+
$this->assertLessThanOrEqual(4, $queryCount);
9292
}
9393

9494
#[Test]

0 commit comments

Comments
 (0)