Post date: Sep 30, 2020 1:45:28 PM
https://www.nicesnippets.com/blog/laravel-tag-system-tutorial-example
The tagging system referenced in the above link is https://github.com/rtconner/laravel-tagging. Although the tutorial in the first link makes it quite easy to install, like other Laravel packages, usage is not so clear. The 2nd link has a list of examples but I feel more detail is needed. Firstly, to introduce tags to your system, you first need a 'tags' text field in your database table. This tags field will be used by the tagging system but accessing it directly is not the way. Once the tags field is in your table, you can add tags to this field by using
<label>Tags L</label>
<input type="text" data-role="tagsinput" name="tags" class-"form-control tags" placeholder="Tags" value="{{ old('tags') }}" />
The above html code is in the blade.php files and together with the script and link declarations in the header (not shown here), they implement the markup for the tagging system. The blade.php file routes the program into the Controller.php file. I am still not totally clear on what to do with the Controller.php files but managed to get two versions running. The first version I used for the store function in the Controller file. This was
$input = $request->all();
$tags = explode(",", $request->get('tags'));
for ($i=0; $i < count($tags); $i++) {
$tags[$i] = trim($tags[$i]);
}
$post = Post::create($input);
$post->tag($tags);
then I when I did the update function, I was quite surprised when all I needed to do was to handle the tags by themselves i.e.
$tags = explode(",", $request->get('tags'));
for ($i=0; $i < count($tags); $i++) {
$tags[$i] = trim($tags[$i]);
}
$post->untag();
$post->tag($tags);
as the $post was already defined earlier on in the update function. For the update.blade,php file,
<label>Tags : </label>
<input type="text" data-role="tagsinput" name="tags"
class="form-control tags" placeholder="Tags"
value="@foreach ($post->tags as $tag) {{ $tag->name . ', ' }} @endforeach"/>
This managed to get the tags into the database table evenly. However, on creating the tag from using create, the field in the table consisted of say tag1, tag2. Once the record had gone through the update function, the field became ["tag1","tag2"]. I don't like it but so far it has not caused any problems.