我有这个错误,谷歌搜索的结果没有一个和我的问题相似。
我有一个带有类Deal、User和Matches的应用程序
一笔交易有很多火柴。一个用户有很多匹配项。一个用户有很多交易。
我试图创建一个新的匹配使用我的交易对象
$deal->matches()->create(['user_id'=>$id]);
这是我的match类,我定义了所有需要的关系
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');
}
}
当我试图创建一个新的匹配项时,我一直会遇到这个错误。
查询onnection.php行647中的异常:SQLSTATE[HY000]:常规错误:1364字段user_id没有默认值(SQL:插入到匹配
(deal_id
)值(1))
我的守卫是空阵,有什么问题吗?
移除受保护的数组,并添加可填充的
数组:
protected $fillable = ['user_id', 'deal_id'];
如果要恢复到以前的行为,请更新
配置/数据库。php
文件和设置'strict'=
因为它在我的情况下是一个独特的领域,我不能使它无效。
对我来说,我有一个空的构造函数导致了这个问题,我不知道为什么。如果有人知道原因,请发表评论。
public function __construct(){
}
评论/删除它解决了问题。