Commit 5a3ebbcb authored by Micheal Mand's avatar Micheal Mand

Prevent race conditions by getting fresh data

If two of the same tag were to be supplied, both would be created (or attempt to be created, because the second one will fail DB unique index and throw an error). We can prevent this by fetching fresh data from the database before checking.
parent 24d1aeb2
......@@ -102,7 +102,7 @@ trait TaggableTrait
}
// Get the current entity tags
$entityTags = $this->tags->pluck($type)->all();
$entityTags = $this->tags()->get()->pluck($type)->all();
// Prepare the tags to be added and removed
$tagsToAdd = array_diff($tags, $entityTags);
......@@ -157,7 +157,7 @@ trait TaggableTrait
$tag->save();
}
if ($this->tags->contains($tag->id) === false) {
if ($this->tags()->get()->contains($tag->id) === false) {
$this->tags()->attach($tag);
}
}
......@@ -167,7 +167,7 @@ trait TaggableTrait
*/
public function untag($tags = null)
{
$tags = $tags ?: $this->tags->pluck('name')->all();
$tags = $tags ?: $this->tags()->get()->pluck('name')->all();
foreach ($tags as $tag) {
$this->removeTag($tag);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment