如何在数据库中保存多个选择?

我有一个多选选项,我想将所有记录保存在数据库中,现在只保存最后一个,我该怎么做?我需要保存,中的多选内容,并用逗号($news = News::create([ 'locale' => Session::get('admin_locale'),'title' => $request['title'],'slug' => Slugify::slugify($request['title']),'news_class' => $request['news_class'],'description' => $request['description'],'tag' => $request['tag'],'tags' => $request->input['tags'],'category' => 'news','category_id' => $request['category_id'],'metatitle' => $request['title'],'metadescription' => substr(strip_tags($request['description']),160),'image' => $image,]); )隔开。 。 这是我的控制人,也是我的尝试

  <div class="row d-flex justify-content-center mt-100 col-md-12 g-mb-30" >
    <div class="col-md-12"   > 
        <label class="g-mb-10">Tags</label>
        <select id="choices-multiple-remove-button" placeholder="Select" multiple title="Category Talent" name="tags">

                @foreach($news as $tag)
                            <option value="{{ $tag->tag }}">{{ $tag->tag }}</option>
                @endforeach 
        </select> </div>
</div>

这是我的观点:

GROUP BY
gh300660 回答:如何在数据库中保存多个选择?

要传递多个值,您可能希望将输入重命名为数组:

<select ... name="tags[]">

然后在服务器端,应在输入tags下将它们作为数组接收:

$tags = $request->input('tags',[]);

您可以使用implode将数组元素连接起来以获得字符串表示形式:

$tags = implode(',',$tags);

PHP Manual - Function Reference - Text Processing - Strings - Functions - implode

$news = News::create([
    ...
    'tags' => implode(',$request->input('tags',[])),...
]);
本文链接:https://www.f2er.com/3146352.html

大家都在问