Krishna Gupta
Krishna Gupta

Reputation: 179

How to save Multiple Selection data in Laravel

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

Answers (3)

Sohel0415
Sohel0415

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

Abid Raza
Abid Raza

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

parth patel
parth patel

Reputation: 141

You can use Bootstrap select to choose multiple select.

https://bootsnipp.com/snippets/Ekd8P

Upvotes: -1

Related Questions