繁体   English   中英

如何在Laravel中更新多对多关系标签

[英]how to update many to many relationship tags in laravel

我刚刚在laravel中制作了一个新应用。 我试图用标签更新我的博客文章。 这是我的表格。

    {{ Form::model($blog, array('route' => array('admin.blog.update', $blog->slug), 'method' => 'PUT', 'files' => true)) }} <!-- text input -->
                   <div class="form-group">
                     {!! Form::label('title', 'Title') !!}
                     {!! Form::text('title', null, ['class' => 'form-control', 'placeholder' => 'Title']) !!}
                   </div>
                    <div class="form-group">
                      {!! Form::label('description', 'Please Type Here Content') !!}
                      {!! Form::textarea('content', null, ['class'=>'ckeditor', 'id'=>'my-editor']) !!}
                    </div>
                    <div class="form-group">
                      <label>Category</label>
                      <select name="tag[]" class="form-control select2" multiple="multiple" data-placeholder="Select Categories"
                              style="width: 100%;">
                        @forelse($tags as $tag)
                        <option>{{ $tag->name }}</option>
                        @empty
                        @endforelse
                      </select>
                    </div>
                   <div class="row">
                      <div class="col-md-4">
                        <div class="form-group">
                        {!! Form::label('published_at', 'Publish On') !!}
                        {!! Form::input('date', 'published_at', date('Y-m-d'), ['class'=>'form-control']) !!}
                      </div>
                     </div>
                   </div>
                    <div class="form-group">
                      {!! Form::label('image', 'Choose Image') !!}
                      {!! Form::file('image') !!}
                    </div>
                    @push('scripts')
                <script src="//cdn.ckeditor.com/4.6.2/standard/ckeditor.js"></script>
                <script>
                  var options = {
                    filebrowserImageBrowseUrl: '{{ url('filemanager')}}?type=Images',
                    filebrowserImageUploadUrl: '{{ url('upload')}}?type=Images&_token={{csrf_token()}}',
                    filebrowserBrowseUrl: '{{ url('filemanager')}}?type=Files',
                    filebrowserUploadUrl: '{{ url('upload')}}?type=Files&_token={{csrf_token()}}'
                  };
                  CKEDITOR.replace('my-editor', options);
                </script>
                <script>
  $(function () {
    //Initialize Select2 Elements
    $('.select2').select2()



    //Timepicker
    $('.timepicker').timepicker({
      showInputs: false
    })
  })
</script>
                @endpush
              </div>
              <div class="modal-footer">
                <button type="button" class="btn btn-default pull-left" data-dismiss="modal">Close</button>
                {!! Form::submit('Submit', array( 'class'=>'btn btn-info' )) !!}
                {!! Form::close() !!}

这是我的控制器

> public function blogeditupdate($blog, Request $request){
>         $blog = Blog::where('slug', $blog)->firstorfail();$blog = new Blog;
>         $blog->title = $request->title;
>         $blog->content = $request->content;
>         $blog->slug = str_slug($blog->title, '-');
>         $blog->user_id = Auth::user()->id;
>         $blog->published_at = Carbon::now();
>         if($request->hasFile('image')) {
>             $file = Input::file('image');
>             //getting timestamp
>             $timestamp = str_replace([' ', ':'], '-', Carbon::now()->toDateTimeString());
>             $name = $timestamp. '-' .$file->getClientOriginalName();
>             $file->move(public_path().'/images/blog/', $name);
>             $blog->image = $name;
>             $thumb = Image::make(public_path().'/images/blog/' . $name)->resize(640,420)->save(public_path().'/images/blog/thumb/' .
> $name, 90);
>         }
>         $blog_id = $blog->id;
>         $tags = $request['tag'];
>         if (isset($tags)) {        
>             $blog->tags()->sync($tags);  //If one or more tags is selected associate blog to tagblog          
>         }        
>         else {
>             $blog->tags()->detach(); //If no tags is selected remove exisiting role associated to a blogs
>         }
>         return redirect()->route('admin.blog.index')->with('status', 'Edit Success');
>     }

这是我的错误

SQLSTATE [23000]:完整性约束违规:1048列'blog_id'不能为空(SQL:插入blog_tagblogblog_idcreated_attagblog_idupdated_at )值(,2017年9月10日十五点17分11秒,ghanta,2017- 09-10 15:17:11))◀“

我错过了一些东西,但是我不知道我在做什么错。请帮助我!

在控制器中第一行的末尾,您有以下内容:

$blog = new Blog;

这意味着$blog将没有id,并且当您尝试设置关系时, blog_id字段将为null ,这违反了您的完整性约束。

如果此行有误,请将其删除。

如果没有,您需要在尝试同步标签之前调用$blog->save()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM