我刚刚在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列&#39; blog_id&#39;不能为空(SQL:插入
blog_tagblog
(blog_id
,created_at
,tagblog_id
,updated_at
)值(,2017-09-10 15:17: 11,ghanta,2017-09-10 15:17:11))◀&#34;
我错过了一些东西,但我不知道自己做错了什么。请帮助我!!
答案 0 :(得分:0)
在控制器第一行的末尾,你有这个:
SUDO_USER
这意味着$blog = new Blog;
没有ID,当您尝试设置关系时,$blog
字段将为blog_id
,这会违反您的完整性限制。
如果此行有误,请将其删除。
如果没有,您需要在尝试同步代码之前致电null
。