tag-it输入仅存储DB laravel 5.4中的最后一个值

时间:2017-03-17 07:02:14

标签: jquery laravel laravel-5.4 tag-it

我正在使用Jquery Tag-it。当我将tag-it值传递给控制器​​并通过var_dump()检查它;所有的值都完美地发布,但问题是,当它传递给DB时,只存储最后一个值 ..请帮帮我



<input type="text" value="" data-role="tagsinput" id="tags" name="wish-tags" class="form-control">
&#13;
&#13;
&#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,

                ]);
            }
        }

1 个答案:

答案 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,
          ]);
       }
 }