简体   繁体   English

SQLSTATE [HY000]:常规错误:1364字段“ mov_id”没有默认值

[英]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 image of error message:- 错误消息的图像:- 在此处输入图片说明

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.

相关问题 Laravel Voyager SQLSTATE [HY000]:常规错误:1364字段“ id”没有默认值 - Laravel Voyager SQLSTATE[HY000]: General error: 1364 Field 'id' doesn't have a default value SQLSTATE[HY000]:一般错误:1364 字段“client_id”没有默认值 - SQLSTATE[HY000]: General error: 1364 Field 'client_id' doesn't have a default value SQLSTATE [HY000]:常规错误:1364字段&#39;author_id&#39;没有默认值 - SQLSTATE[HY000]: General error: 1364 Field 'author_id' doesn't have a default value Laravel SQLSTATE [HY000]:常规错误:1364字段“ id”没有默认值 - Laravel SQLSTATE[HY000]: General error: 1364 Field 'id' doesn't have a default value SQLSTATE [HY000]:常规错误:1364字段“ reservation_id”没有默认值 - SQLSTATE[HY000]: General error: 1364 Field 'reservation_id' doesn't have a default value SQLSTATE [HY000]:一般错误:1364 字段“agent_id”没有默认值 - SQLSTATE[HY000]: General error: 1364 Field 'agent_id' doesn't have a default value SQLSTATE[HY000]:一般错误:1364 字段“parent_id”没有默认值 - SQLSTATE[HY000]: General error: 1364 Field 'parent_id' doesn't have a default value SQLSTATE[HY000]:一般错误:1364 字段“country_id”没有默认值 - SQLSTATE[HY000]: General error: 1364 Field 'country_id' doesn't have a default value SQLSTATE [HY000]:一般错误:1364 字段“trans_id”没有默认值 - SQLSTATE[HY000]: General error: 1364 Field 'trans_id' doesn't have a default value SQLSTATE[HY000]:一般错误:1364 字段“care”没有默认值 - SQLSTATE[HY000]: General error: 1364 Field 'care' doesn't have a default value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM