【发布时间】:2017-11-13 06:05:53
【问题描述】:
我被这个错误吸引了,因为这周有人可以帮我吗!
SQLSTATE[23000]:完整性约束违规:1452 无法添加或更新子行:外键约束失败(practice2.posts, CONSTRAINT posts_category_id_foreign FOREIGN KEY (category_id) REFERENCES categories (@987654326 @)) (SQL: 插入posts (author_id, updated_at, created_at) 值 (2, 2017-11-13 05:48:53, 2017-11-13 05:48:53) )
我的代码是: 博客控制器:
public function store(Requests\PostRequest $request)
{
$request->user()->posts()->create($request->all());
redirect('/backend/blog')->with('message', 'Your post was created successfully!');
}
**migration:**
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->increments('id');
$table->integer('author_id')->unsigned();
$table->foreign('author_id')->references('id')->on('users')->onDelete('restrict');
$table->string('tittle');
$table->string('slug')->unique;
$table->text('excerpt');
$table->text('body');
$table->string('image')-> nullable();
$table->timestamps();
});
}
alter_post_migration:
public function up()
{
Schema::table('posts', function (Blueprint $table) {
$table->integer('category_id')->unsigned();
$table->foreign('category_id')->references('id')->on('categories')->onDelete('restrict');
//
});
}
创建帖子表单:
<div class="box-body ">
{!! Form::model($post, [
'method' => 'POST',
'route' => 'back.store'
]) !!}
<div class="form-group {{ $errors->has('tittle') ? 'has-error' : '' }}">
{!! Form::label('tittle') !!}
{!! Form::text('tittle', null, ['class' => 'form-control']) !!}
@if($errors->has('tittle'))
<span class="help-block">{{ $errors->first('tittle') }}</span>
@endif
</div>
<div class="form-group {{ $errors->has('slug') ? 'has-error' : '' }}">
{!! Form::label('slug') !!}
{!! Form::text('slug', null, ['class' => 'form-control']) !!}
@if($errors->has('slug'))
<span class="help-block">{{ $errors->first('slug') }}</span>
@endif
</div>
<div class="form-group">
{!! Form::label('excerpt') !!}
{!! Form::textarea('excerpt', null, ['class' => 'form-control']) !!}
</div>
<div class="form-group {{ $errors->has('body') ? 'has-error' : '' }}">
{!! Form::label('body') !!}
{!! Form::textarea('body', null, ['class' => 'form-control']) !!}
@if($errors->has('body'))
<span class="help-block">{{ $errors->first('body') }}</span>
@endif
</div>
<div class="form-group {{ $errors->has('published_at') ? 'has-error' : '' }}">
{!! Form::label('published_at', 'Publish Date') !!}
{!! Form::text('published_at', null, ['class' => 'form-control', 'placeholder' => 'Y-m-d H:i:s']) !!}
@if($errors->has('published_at'))
<span class="help-block">{{ $errors->first('published_at') }}</span>
@endif
</div>
<div class="form-group {{ $errors->has('category_id') ? 'has-error' : '' }}">
{!! Form::label('category_id', 'Category') !!}
{!! Form::select('category_id', App\Category::pluck('tittle', 'id'), null, ['class' => 'form-control', 'placeholder' => 'Choose category']) !!}
@if($errors->has('category_id'))
<span class="help-block">{{ $errors->first('category_id') }}</span>
@endif
</div>
<hr>
{!! Form::submit('Create new post', ['class' => 'btn btn-primary']) !!}
{!! Form::close() !!}
</div>
【问题讨论】:
-
错误很明显 - 您正在尝试插入没有有效
category_id的posts记录。很可能将category_id默认为空,并且没有空category_id行。建议添加默认类别(例如 0)并将未分类的帖子设置为此,或更改posts.category_id以允许空值。 -
我后来修改了表,posts表中有category_id字段
标签: mysql laravel-eloquent laravel-5.5