2

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.

2
  • 2
    what's wrong with this code? Commented Nov 7, 2017 at 11:48
  • drop here the html for the options of your select and i will tell you Commented Nov 7, 2017 at 11:57

3 Answers 3

2

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 :)

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

-1

You can use Bootstrap select to choose multiple select.

https://bootsnipp.com/snippets/Ekd8P

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.