【发布时间】:2019-11-07 08:56:58
【问题描述】:
我有一个多选选项,我想将所有记录保存在数据库中,现在它只保存最后一个,我该怎么做?我需要使用逗号 (,) 从 tags 保存多选。 .
这是我的控制器和我尝试的方法
$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']), 0, 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>
【问题讨论】:
-
您的
tags字段是什么类型的?我假设您的意思是$request->input('tags')而不是$request->input['tags']? -
是
varchar(255)。 -
那么您想如何将多个标签保存到该字段中?逗号分隔列表?
-
中间有逗号。 (
tag1,tag2,tag3)。