[英]SQLSTATE[HY000]: General error: 1364 Field 'mov_id' doesn't have a default value
have a question related with the 2 tables which are movie and categories 有一个与电影和类别这两个表有关的问题
categories are belong to movie which mean i can set many kind of categories for the movie. 类别属于电影,这意味着我可以为电影设置多种类别。
So in the categories models, I put this code 所以在类别模型中,我将这段代码
public function movie(){
return $this->belongsTo('App\Movie', 'mov_id');
}
which I want to set categories.mov_id = movie.id 我要设置的category.mov_id = movie.id
On Controller Part, validation is fine off course show the error message when i put blank. 在控制器部分,当我放空白时,验证很好,当然会显示错误消息。 But the problem is cant save to database and display mov_id doesn't have a default value
但是问题是无法保存到数据库并且显示mov_id没有默认值
public function store(Request $request)
{
$rules = array(
'cat_title' => 'required'
);
$validator = Validator::make(Input::all(), $rules);
if ($validator->fails()) {
return Redirect::to('category/create')
->withErrors($validator);
} else {
$movie = new Category();
$movie->cat_title = Input::get('cat_title');
$movie->save();
Session::flash('message', 'Successfully created!');
return Redirect::to('category');
}
}
and last, here is the view file 最后,这是查看文件
<div class="table-container">
<form method="POST" action="{{ route('category.store') }}" role="form">
{{ csrf_field() }}
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<input type="text" name="cat_title" id="cat_title" class="form-control input-sm" placeholder="分类">
</div>
</div>
</div>
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12">
<input type="submit" value="Save" class="btn btn-success btn-block">
<a href="{{ route('category.index') }}" class="btn btn-info btn-block" >Back</a>
</div>
</div>
</form>
</div>
Category schema 类别架构
Schema::create('category', function (Blueprint $table) {
$table->increments('id');
$table->string('cat_title');
$table->integer('mov_id');
$table->timestamps();
});
The error msg tells you exactly what the problem is. 错误消息会告诉您确切的问题是什么。 You are trying to insert a category record, but your form does not include a
mov_id
value. 您正在尝试插入类别记录,但是您的表单不包含
mov_id
值。 Your schema specifies that mov_id
is an integer, but does not specify any default value, nor that it can be null. 您的架构指定
mov_id
为整数,但不指定任何默认值,也不能为null。 So Laravel does not know what to use for mov_id
if you don't tell it, so it can't create a record. 因此,如果您不告知Laravel,则不知道将其用于
mov_id
,因此它无法创建记录。
The quick fix is to update your schema to allow null values for mov_id
: 快速解决方案是更新您的架构以允许
mov_id
为空值 :
$table->integer('mov_id')->nullable();
But I think you're going to have bigger problems. 但是我认为您将会遇到更大的问题。 A category probably should not have a movie ID at all - that would mean that every category could have at most 1 movie.
一个类别可能根本不应该有电影ID-这意味着每个类别最多只能有1个电影。
I think what you really want is a many-to-many relationship between movies and categories. 我认为您真正想要的是电影和类别之间的多对多关系。 That means one movie can have many categories, and one category can be assigned to many movies.
这意味着一部电影可以具有多个类别,并且一个类别可以分配给许多电影。
The Laravel docs describe it well and include an example . Laravel文档对此进行了很好的描述,并提供了一个示例 。 You'll need to update your Laravel relationships:
您需要更新您的Laravel关系:
// Category model
public function movie() {
return $this->belongsToMany('App\Movie');
}
// Movie model
public function category() {
return $this->belongsToMany('App\Category');
}
And your schema: 和您的架构:
Schema::create('movies', function (Blueprint $table) {
$table->increments('id');
$table->string('title');
// ... etc
});
Schema::create('categories', function (Blueprint $table) {
$table->increments('id');
$table->string('cat_title');
// ... etc
});
Schema::create('category_movie', function (Blueprint $table) {
$table->integer('category_id');
$table->integer('movie_id');
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.