Why with attachTag method of spatie/laravel-tags I got 2 rows in tags table?

101 Views Asked by At

In laravel 9 with spatie/laravel-tags 4.3 I add some initial data in seeder tagged with code like :

        $this->votes_tag_type= 'votesTagType';
        $lNewVote= Vote::create([
            'id'               => 1,
            'name'             => 'To be or not to be ?',
            'slug'             => 'to-be-or-not-to-be',
            'description'      => 'Still trying to find an answer. is the opening phrase of a soliloquy spoken by <i><i>Prince <i>Hamlet</i></i></i> in the so-called "nunnery scene" of <i><i>William Shakespeare</i>\'s</i> play <i>Hamlet</i>. Act III, Scene I.Though it is called a soliloquy <i>Hamlet</i> is not alone when he makes this speech because Ophelia is on stage pretending to read while waiting for <i>Hamlet</i> to notice her, and Claudius and Polonius, who have placed Ophelia in <i><i>Hamlet</i>\'s</i> way in order to overhear their conversation and find out if <i>Hamlet</i> is really mad or only pretending, have concealed themselves. Even so, <i>Hamlet</i> seems to consider himself alone and there is no indication that the others on stage hear him before he addresses Ophelia. In the speech, <i>Hamlet</i> contemplates death and suicide, bemoaning the pain and unfairness of life but acknowledging that the alternative might be worse. The meaning of the speech is heavily debated but seems clearly concerned with <i><i>Hamlet</i>\'s</i> hesitation to avenge his father\'s murder (discovered in Act I) by his uncle Claudius.
 Lorem  ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod  tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim  veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea  commodo consequat. Duis aute irure dolor in reprehenderit in voluptate  velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint  occaecat cupidatat non proident, sunt in culpa qui officia deserunt...',
            'creator_id'       => 3,
            'vote_category_id' => 1,   // Classic literature
            'is_quiz'          => false,
            'is_homepage'      => true,
            'ordering'         => 1,
            'status'           => 'A',
            'meta_description' => 'William Shakespeare with eternal question',
            'meta_keywords'    => ['Hamlet', 'William Shakespeare', 'Theater'],
            'image'            => 'tobe.png',
        ]);

        $newTag= SpatieTag::findOrCreate('Hamlet', $this->votes_tag_type);
        $lNewVote->attachTag('Hamlet', $this->votes_tag_type);

But with attachTag method used above I see 2 rows in tags table : https://prnt.sc/HzSVdpf0zDlf also in taggables table I see rows with tag_id = 2. Assigned to 2nd row in tags table. I think that is not valid, but what is wrong ?

Thanks!

1

There are 1 best solutions below

0
mstdmstd On BEST ANSWER

Valid code is:

$lNewVote->syncTagsWithType(['Hamlet', 'William Shakespeare', 'Drama', 'Theater'],  $this->votes_tag_type);