I am trying to implement a tagging feature in Laravel application.
Implementation consist of tag cloud which is fetched using ajax request to recommend tags also there is an option to create new tag if a tag is not present. In backend I want to get all the tag names.
Steps to produce result:
JS code
$('#tags').select2({
tags: true,
tokenSeparators: [',', ' '],
minimumInputLength: 2,
ajax: {
url: '{{ route("tags.search") }}',
},
placeholder: 'Search for a category',
data: function (params) {
return {
q: $.trim(params.term)
};
},
processResults: function (data) {
console.log(data);
return {
results: data
};
},
cache: true
});
HTML code
<div class="form-group col-sm-12 col-md-6">
<label for="tags">Tags</label>
<select class="js-example-basic-multiple select2-purple" name="tags[]" multiple id="tags" style="width: 100%">
<option value="AL">PHP</option>
<option value="WY">JAVA</option>
</select>
</div>
Requset object outupt:
tags: array['0'->'1','1'->'test']
Values: 0->1(oldtag) is for ajax returned value, 1->test for new value not present in cloud tag
Expected Output
tags: array['0'->'oldtag','1'->'newtag']