如何使用select 2(plugin)手动和从数据库动态保存标签

时间:2019-04-11 19:31:26

标签: php laravel jquery-select2

我可以存储数据库中的标签,但是当我尝试手动输入标签时,由于新标签未存储在数据库中,所以我无法这样做。

到目前为止,我该怎么办

我的控制器来存储数据

def event(request, event_id=None):
    instance = Event()

    if event_id:
        instance = get_object_or_404(Event, pk=event_id)
    else:
        instance = Event()

    form = EventForm(request.POST or None, instance=instance)

    if request.POST and form.is_valid():
        event = Event.objects.create(**form.cleaned_data, user=request.user)
        print(event.title)
        return HttpResponseRedirect(reverse('cal:calendar'))
    return render(request, 'cal/event.html', {'form': form})

我的表格

 $data = $this->handleRequest($request);//for handle image request
 $newPost = $request->user()->posts()->create($data);
 $newPost->tags()->sync($request->tags, false);

和脚本

{!!Form::select('Tags',$tags->pluck('name','id')->all(),null,array(
                'multiple' => 'multiple',
                'class'    => 'form-control select2-multi',
                'name'     => 'tags[]'))
            !!}

0 个答案:

没有答案