Reputation: 749
I am using wenzhixin/multiple-select as it has select all option. I'm trying to get selected items ticked that were selected while creating the post.
This is what i have done but it doesn't work.
{{ Form::label('tags', 'Tags:') }}
<select name="tags[]" multiple="multiple" id="tags">
@foreach($tags as $tag)
<option value="{{$tag->id}}">{{$tag->name}}</option>
@endforeach
</select>
<script>
$("#tags").multipleSelect({
placeholder: "Tags please"
});
$('#tags').multipleSelect().val({!!json_encode($post->tags()->getRelatedIds() )!!}).trigger('change');
</script>
I would be thakful if anyone can help me.
Upvotes: 0
Views: 2027
Reputation: 50798
At the end of the day, it's just an HTML select element. Given the name of tags[]
, this will submit within the form and be accessible through a controller as $request->get('tags')
or if using the helper, then request()->get('tags');
Furthermore, you can pass the selected items to the next page in this way:
public function myFunctionToHandleTheForm()
{
$tags = App\Tag::all(); //for example, no idea what you use
$selectedTags = request()->get('tags');
return view('my.view', compact('selectedTags', 'tags');
}
Now, $tags
will be a numerically ordered array (ascending) containing the ID's
that were submitted from your form. You can set the defaults that were checked just like this:
{{ Form::label('tags', 'Tags:') }}
<select name="tags[]" multiple="multiple" id="tags">
@foreach($tags as $tag)
<option value="{{$tag->id}}" {{ in_array($tag->id, $selectedTags) ? 'selected="selected"' : null }}>{{$tag->name}}</option>
@endforeach
</select>
In the above, we're checking if the tag id is in the list of selected tag id's
, and if so, we're setting the selected
attribute. Your plugin will understand this and translate it correctly.
Upvotes: 1