where are you storing the tags ?
Dec 4, 2017
6
Level 1
Select2 Tags Edit Post
Hi i created a tag functionality for my posts so when i create a new post i dont have any problem and here is my code
<div style="width:250px; !important;" class="search__wrapper styled-select color rounded">
<span style="color:white !important;" class="search__item__icon--category ion-pricetag"></span>
<select name="tag_list[]" id="tag_list" class="select2-multi" multiple="multiple">
@foreach ($tags as $tag)
<option value="{{$tag->id}}">{{$tag->name}}</option>
@endforeach
</select>
</div>
But the problem is when i edit a post i cant show the selected tags for the specific post here is my code for edit post
<div style="width:250px; !important;" class="search__wrapper styled-select color rounded">
<span style="color:white !important;" class="search__item__icon--category ion-pricetag"></span>
<select name="tag_list[]" id="tag_list" class="select2-multi" multiple="multiple">
@foreach ($tags as $tag)
<option value="{{$tag->id}}">{{$tag->name}}</option>
@endforeach
</select>
</div>
Level 2
Okay here are the steps to follow.
-
in edit function of controller get all the tags of particular article and pass them to view.
-
In view check in your for loop of select if that id exists in tags array, if it exists use 'selected' attribute.
@foreach ($tags as $tag) <option value="{{$tag->id}}" {{ in_array($tag->id,$selected_tags)?'selected':''}}> {{$tag->name}}</option> @endforeach
3 likes
Please or to participate in this conversation.