attachTags fails when trying to attach new tags that already exist in the database in Laravel v10.23.1
theshubhamchugh opened this issue · 1 comments
While working with Laravel v10.23.1 (PHP v8.2.10), I've come across an issue when using the attachTags method. Specifically, when I try to create and attach new tags to a model, everything seems to function as expected. But New tag not found in databse.However, if the new tag I'm attempting to create already exists in the database, I encounter an error.
Illuminate\Database\QueryException SQLSTATE[HY000]: General error: 1366 Incorrect integer value: '' for column 'tag_id' at row 1
(Connection: mysql, SQL: insert into `taggables` (`created_at`, `tag_id`, `taggable_id`, `taggable_type`, `updated_at`) values (2023-09-17 12:36:16, , 1, App\Models\Blog\BlogPost, 2023-09-17 12:36:16)).The error suggests that the tag_id is not being populated correctly when the tag already exists in the database.
Steps to Reproduce:
- Create a new model instance.
- Use attachTags to attach a new tag to the model. This works without any issues.But New Tags Not found in databse.
- Attempt to use attachTags again with a tag name that already exists in the database.
Expected Result:
The existing tag should be attached to the model without any issues.
Actual Result:
An error is thrown highlighting an incorrect integer value for the tag_id column.
screen-recording-2023-09-17-at-60538-pm_EbLoyocq.mp4
In the meantime, I'd suggest switching to https://github.com/spatie/laravel-tags
It provides almost all functionality and is actively maintained.