Answer the question
In order to leave comments, you need to log in
How to implement "tags" in laravel?
Good day to all. How to implement a tagging system for a post? As I understand it, the table structure should be as follows: posts (records), tags and a list of posts and tags of the form - post_id | tag_id .
It turns out that on the post creation page, the user enters tags separated by commas in a line, then this line is sent to the post controller and there it is split into separate tags, more precisely into an array. But how to check for the presence of the same tags in the database from the controller, get their id, and the id of new ones after adding them (this is done according to the rules in the tag controller, respectively), and then write it to the table of posts and tags ( post_id | tag_id ). PSexplode(',', $str)
I'm not asking for code, I'm asking for at least a small manual on what functions to use and in which direction to start. Thanks in advance.
Answer the question
In order to leave comments, you need to log in
The answer to your question is in the laracasts fundamentals core course. Everything is short and to the point:
Many to Many Relations (With Tags)
the user enters tags in a line separated by commasisn't that too primitive?
Do it like on a toaster, select2 +ajax to the rescue. Fold the selected tags into <input type="hidden" name="tags[]">
, so when submitting the form, you will get an array that will be very easy to work with on the backend.
Do you know how to use composer? Here is the finished solution https://github.com/rtconner/laravel-tagging
It doesn't matter how the user submits the tags. What matters is how you process them. Everything is done extremely simply:
get an array of tags and attach them to the desired model. ALL! The stall itself will check what needs to be done and will do it.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question