php Laravel 5.4 字段没有默认值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43186521/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 02:47:33  来源:igfitidea点击:

Laravel 5.4 field doesn't have a default value

phpmysqllaravellaravel-5.4

提问by John David

I am having this error and none of the googled result i checked is similar to my problem.

我遇到了这个错误,我检查的谷歌搜索结果都与我的问题不相似。

I have an application with class Deal, User, and Matches

我有一个包含 Deal、User 和 Matches 类的应用程序

A deal has many matches. A user has many matches. A user has many deals.

一笔交易有很多场比赛。一个用户有很多匹配。一个用户有很多交易。

I am attempting to create a new Match using my Deal object

我正在尝试使用我的 Deal 对象创建一个新的匹配

$deal->matches()->create(['user_id'=>$id]);

This is my match class, i have defined all needed relationships

这是我的比赛班级,我已经定义了所有需要的关系

class Match extends Model
{
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $guarded = [];
    public $timestamps = false;
    public $expired_on = "";


    public static function boot()
    {
        parent::boot();

        static::creating(function ($model) {
            $model->matched_on = $model->freshTimestamp();
        });
    }

    public function __construct(){
        $d = (new \DateTime($this->matched_on))->modify('+1 day');
        $this->expired_on = $d->format('Y-m-d H:i:s');
    }


    /**
     * Get the user that owns the match.
     */
    public function user()
    {
        return $this->belongsTo('App\User');
    }

    /**
     * Get the deal that owns the match.
     */
    public function deal()
    {
        return $this->belongsTo('App\Deal');
    }
}

And i keep getting this error when i attempt to create a new match.

当我尝试创建新匹配时,我不断收到此错误。

QueryException in Connection.php line 647: SQLSTATE[HY000]: General error: 1364 Field 'user_id' doesn't have a default value (SQL: insert into matches(deal_id) values (1))

Connection.php 第 647 行中的 QueryException: SQLSTATE[HY000]: General error: 1364 Field 'user_id' does not have a default value (SQL: insert into matches( deal_id) values (1))

I have my guarded to be an empty array, what could be the problem?

我有我的守卫是一个空数组,可能是什么问题?

回答by Alexey Mezenin

Remove the guardedarray and add the fillableinstead:

删除guarded数组并添加fillable

protected $fillable = ['user_id', 'deal_id'];

回答by grantDEV

If you would like to revert to previous behavior, update your

如果您想恢复到以前的行为,请更新您的

config/database.php

配置/数据库.php

file and set 'strict' => falsefor your connection.

文件并设置'strict' => false为您的连接。

回答by vivek takrani

Since it was a unique field in my case, I could not make it nullable.

由于在我的情况下它是一个独特的字段,因此我无法将其设为可空。

For me, I had an empty constructor which was causing the issue don't know why. Please comment if anyone knows the reason.

对我来说,我有一个空的构造函数,它导致了这个问题,不知道为什么。如果有人知道原因,请发表评论。

public function __construct(){

}

Commenting/removing it resolved the issue.

评论/删除它解决了这个问题。

回答by John David

Alexey Mezenin'sAnswer is correct and a good one.

Alexey Mezenin 的回答是正确的,而且很好。

Another way i used around it, for those who want to maintain the guarded empty array is to create a new Match object and put in the attributes and save.

对于那些想要维护受保护的空数组的人,我使用的另一种方法是创建一个新的 Match 对象并放入属性并保存。

            $match->user_id = $id;
            $match->deal_id = $deal->id;
            $match->matched_on = $match->freshTimestamp();
            $match->save();

回答by davee44

If you have a constructor in your model, just make sure it has a call to a parent constructor as well:

如果您的模型中有一个构造函数,只需确保它也调用了父构造函数:

public function __construct( array $attributes = array() ) {
    // mandatory
    parent::__construct($attributes);

    //..
}

Otherwise, it will break some functionality like Model::create.

否则,它会破坏某些功能,例如Model::create.