Laravel 5 - SQL 时间戳错误 M:N 关系

Laravel 5 - SQL error on Timestamps with M:N relationship

我有一个 table 模型 User 和另一个模型 Feature

我希望用户对功能进行投票,因此我为其创建了一个 table:

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]: Integrity constraint violation: 19 NOT NULL constraint failed: votes.created_at (SQL: insert into "votes" ("feature_id", "user_id") values (1, 1))

如果我阅读邮件,似乎 created_at 字段没有被填写,但这些字段通常由 Laravel 自动处理。

我搞砸了什么?

如果您要在数据透视表上使用时间戳table,您必须在模型方法中声明。

public function votes()
{
    return $this->belongsToMany('App\User', 'votes')->withTimestamps();
}