Reputation: 179
Can anyone please help me how to save multiple selection in the DB
?
<div class="col-sm-10">
<select id="tag_list" name="tag_list[]" class="form-control" multiple></select>
</div>
Controller function is like this:
public function store(Request $request)
{
$comics = new Comic();
$tags = $request->input('tag_list');
$comics->appreance = implode(',', $tags);
$comics->save();
return redirect('/comic');
}
Please help, thanks.
Upvotes: 2
Views: 6152
Reputation: 9853
Although it's not clear enough how you save your tags or appearance
, i assume
it save one tag in a single
row. If that's the case then you can do something like
public function store(Request $request)
{
$tags = $request->input('tag_list');
foreach($tags as $tag){
$comics = new Comic();
$comics->appreance = tag;
$comics->save();
}
return redirect('/comic');
}
Hope this helps :)
Upvotes: 2
Reputation: 755
Have you tried json?
Try this
public function store(Request $request)
{
$comic = new Comic();
$tags = $request->input('tag_list');
$comic->appreance = json_encode($tags);
$comic->save();
return redirect('/comic');
}
But I'll suggest to consider separate table for tags and create a relationship for comic and tags.
Take a look at this https://laravel.com/docs/5.3/eloquent-relationships#many-to-many-polymorphic-relations
Upvotes: 0
Reputation: 141
You can use Bootstrap select to choose multiple select.
https://bootsnipp.com/snippets/Ekd8P
Upvotes: -1