我有一個模型User
和另一個模型Feature
表。Laravel 5 - 具有M:N關係的時間戳上的SQL錯誤
我希望用戶對功能進行投票,所以我創建一個表吧:
public function up()
{
Schema::create('votes', function(Blueprint $table) {
$table->increments('id');
$table->integer('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->integer('feature_id')->unsigned();
$table->foreign('feature_id')->references('id')->on('features')->onDelete('cascade');
$table->timestamps();
});
}
我定義的Feature
模型的關係:
public function votes()
{
return $this->belongsToMany('App\User', 'votes');
}
在控制器中,我嘗試對用戶登錄進行投票。
public function vote(Feature $feature) {
$feature->votes()->save(Auth::user());
}
但是當我執行此操作時,噸以下消息:
SQLSTATE [23000]:完整性約束違規:19 NOT NULL約束失敗:votes.created_at(SQL:插入到 「內容」( 「FEATURE_ID」, 「USER_ID」)值(1, 1))
如果我閱讀郵件,看起來好像created_at
字段沒有被填充,但這些字段通常由Laravel自動處理。
我搞亂了什麼?
你能確認''Feature''模型中沒有''$ timestamps = false;''嗎? – 2015-04-03 14:04:38
既沒有在'Feature'也沒有'User' – 2015-04-03 14:05:29