Reputation: 339
I have a table news
with id
, title
, body
and another table tags
with id
, name
. These two tables are related by a pivot table news_table
with news_id
, tag_id
. Relations are created on modals and foreign key constraints are set on the pivot table. I have a controller manageData
containing a function insertNews
and a form in a view where i get the data entered.
What i want to do is have a field in my form like a text input field which will act like a dropdown where i can add multiple words as tags which will be taken from the tags table and shown to me as suggestions. if the word doesn't exist in my tags table i want it to be added there
I have tried some methods but with no success
Upvotes: 0
Views: 383
Reputation: 163798
You can use one of the many JS scripts with suggestion feature to allow users to choose or add tags, like this one.
When a form is submitted, just iterate over the tags, submitted by a user and build an array of IDs of the existing tags and an array with new tag names. Then use sync()
method to sync existing tags and attach()
method to add new tags.
Or just sync()
method with one array which will have both IDs and tag names.
https://laravel.com/docs/5.4/eloquent-relationships#updating-many-to-many-relationships
Upvotes: 1