Laravel 5.4 字段没有默认值
问题说明
我遇到了这个错误,我检查过的谷歌搜索结果都与我的问题不相似.
I am having this error and none of the 谷歌d result i checked is similar to my problem.
我有一个包含 Deal、User 和 Matches 类的应用程序
I have an application with class Deal, User, and Matches
一笔交易有很多匹配项.一个用户有很多匹配.一个用户有很多交易.
A deal has many matches. A user has many matches. A user has many deals.
我正在尝试使用我的 Deal 对象创建一个新的匹配
I am attempting to create a new Match using my Deal object
$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('AppUser');
}
/**
* Get the deal that owns the match.
*/
public function deal()
{
return $this->belongsTo('AppDeal');
}
}
当我尝试创建新匹配项时,我不断收到此错误.
And i keep getting this error when i attempt to create a new match.
Connection.php 第 647 行中的 QueryException:SQLSTATE[HY000]:一般错误:1364 字段user_id"没有默认值(SQL:插入matches
(deal_id
)值(1))
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))
我的守卫是一个空数组,可能是什么问题?
I have my guarded to be an empty array, what could be the problem?
正确答案
移除 guarded
数组并添加 fillable
代替:
Remove the guarded
array and add the fillable
instead:
protected $fillable = ['user_id', 'deal_id'];
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /reply/detail/tangibkif
-
YouTube API 不能在 iOS (iPhone/iPad) 工作,但在桌面浏览器工作正常?
it1352 07-30 -
iPhone,一张图像叠加到另一张图像上以创建要保存的新图像?(水印)
it1352 07-17 -
保持在后台运行的 iPhone 应用程序完全可操作
it1352 07-25 -
使用 iPhone 进行移动设备管理
it1352 07-23 -
在android同时打开手电筒和前置摄像头
it1352 09-28 -
扫描 NFC 标签时是否可以启动应用程序?
it1352 08-02 -
检查邮件是否发送成功
it1352 07-25 -
Android微调工具-删除当前选择
it1352 06-20 -
希伯来语的空格句子标记化错误
it1352 06-22 -
Android App 和三星 Galaxy S4 不兼容
it1352 07-20