私はこのエラーが発生しており、私がチェックしたグーグルの結果は私の問題と似ていません。Laravel 5.4フィールドにデフォルト値がありません
私は、ユーザーをクラスディールでアプリケーションを持っている、と
取引が多くの試合を持って一致します。 ユーザーには多くの一致があります。 ユーザーには多くの取引があります。
私はこれが私のマッチクラスで、私は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');
}
}
必要なすべての関係を定義していると私はときに私、このエラーを取得しておく
$deal->matches()->create(['user_id'=>$id]);
私ディールオブジェクトを使用して、新しいマッチを作成しようとしています
新しいマッチを作成しようとします。
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))
私は空の配列であることを守っていますが、何が問題なのでしょうか?
ピボットテーブルの場合、matchesにモデルクラスがある理由は次のとおりです。https://laravel.com/docs/5.0/eloquent#working-with-pivot-tables – Indra
これは通常、 NULLにすることができないか、デフォルト値を持たない特定のフィールドを挿入しないでください。あなたの 'user_id'は挿入されません – Cr1xus
@ Cr1xus、はい私の 'user_id'は挿入されません、私の問題は今なぜです、それは私の作成メソッドでそれを見ることです。 –