我正在使用Jquery Tag-it。当我将tag-it值传递给控制器并通过var_dump()检查它;所有的值都完美地发布,但问题是,当它传递给DB时,只存储最后一个值 ..请帮帮我
<input type="text" value="" data-role="tagsinput" id="tags" name="wish-tags" class="form-control">
&#13;
控制器
$wishlist = Wishlist::firstOrNew(['user_id' => Auth::user()->id]);
if (!empty($wishlist)) {
$locations = $request->input('wish-tags');
//var_dump($locations); exit();
foreach (explode(",", $locations) as $location) {
$wishlist->update([
'user_id' => Auth::user()->id,
'locations' => $location,
]);
}
}
答案 0 :(得分:0)
试试这个:
$locations = $request->input('wish-tags');
//var_dump($locations); exit();
foreach (explode(",", $locations) as $location) {
$wishlist = Wishlist::firstOrNew(['user_id' => Auth::user()->id, 'locations' => $location ]);
if (!empty($wishlist)) {
$wishlist->update([
'user_id' => Auth::user()->id,
'locations' => $location,
]);
}
}