gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 13:59:05 24 4
gpt4 key购买 nike

我似乎对时间戳有问题 - 尝试附加时 UserLectures通过 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']);

});

讲课关系:
public function favorites()
{
return $this->belongsToMany(User::class, 'favorites');
}

用户关系:
public function favorites()
{
return $this->belongsToMany(Lecture::class, 'favorites')
->withTimestamps();
}

每当我附上:
$event->lecture->favorites()->attach($this->user->id);

两个字段 created_atupdated_at设置为 0000-00-00 00:00:00
知道我可能做错了什么吗?

最佳答案

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

因此,对于考试,在您的情况下,将是以下内容:

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

这将附加时间戳。希望这对你有帮助。

关于带有时间戳的 Laravel 5.1 eloquent::attach() 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33740693/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com