問題描述
我遇到了這個錯誤,我檢查過的谷歌搜索結果都與我的問題不相似.
I am having this error and none of the googled 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'];
這篇關于Laravel 5.4 字段沒有默認值的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!