F
F
flafy42016-09-25 22:31:03
Laravel
flafy4, 2016-09-25 22:31:03

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

5 answer(s)
A
alveryu, 2016-10-05
@alvery

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)

C
coderisimo, 2016-09-25
@coderisimo

the user enters tags in a line separated by commas
isn't that too primitive?
you can make it prettier on the front-end. For example, use multiselect - www.w3schools.com/tags/tryit.asp?filename=tryhtml_... (although there are prettier solutions. For example, see from the 17th second https://dl.dropboxusercontent.com/u/19954007 /work/... . I did it myself, so you can too ))) )
In the case of select multiple, you will not need to parse the string. You just get an array with tag IDs and you can work with this array as you like.

T
Tesla, 2016-09-26
@Tesla

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.

2
2vtlk, 2016-09-26
@2vtlk

Do you know how to use composer? Here is the finished solution https://github.com/rtconner/laravel-tagging

A
Alex Wells, 2016-09-28
@Alex_Wells

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 question

Ask a Question

731 491 924 answers to any question