带有时间戳的Laravel 5.1雄辩的::: attach()方法

Seb*_*ski 4 laravel eloquent laravel-5.1

我似乎有时间戳问题-试图连接时User,以Lectures通过favorites透视表-没有日期的更新。

这是我的迁移:

Schema::create('favorites', function (Blueprint $table) {

    $table->integer('lecture_id')->unsigned();
    $table->integer('user_id')->unsigned();
    $table->timestamps();

    $table->primary(['lecture_id', 'user_id']);

});
Run Code Online (Sandbox Code Playgroud)

讲座关系:

public function favorites()
{
    return $this->belongsToMany(User::class, 'favorites');
}
Run Code Online (Sandbox Code Playgroud)

用户关系:

public function favorites()
{
    return $this->belongsToMany(Lecture::class, 'favorites')
                ->withTimestamps();
}
Run Code Online (Sandbox Code Playgroud)

每当我附上:

$event->lecture->favorites()->attach($this->user->id);
Run Code Online (Sandbox Code Playgroud)

两个字段created_atupdated_at都设置为0000-00-00 00:00:00

知道我做错了什么吗?

Pho*_*rce 7

为了将时间戳附加到每个模型关系,您需要链接方法->withTimestamps()

因此,对于您而言,对于考试而言,应为以下内容:

public function favorites()
{
    return $this->belongsToMany(User::class, 'favorites')->withTimestamps(); 
}
Run Code Online (Sandbox Code Playgroud)

然后,这将附加时间戳。希望这对您有所帮助。