有没有一种方法可以使用 touch()
更新表中 is_online
字段的时间戳,而不是更新 中的 created_at
字段laravel Eloquent ORM
目前我正在使用
User::where('id',$senderId )->update(array('is_online' => date('Y-m-d H:i:s')));
最佳答案
不,touch 方法不是为更新内置时间戳以外的任何内容而编写的,但如果您愿意,您可以在用户模型中编写自己的函数。像这样
class User extends Eloquent implements UserInterface, RemindableInterface {
public function touchOnline()
{
$this->is_online = $this->freshTimestamp();
return $this->save();
}
}
然后用
替换你的旧代码User::find($senderId)->touchOnline();
多了几行代码,但可读性可能会稍微好一点。
您可以 find the code behind the touch function here ,如果你好奇的话。
关于php - 使用 touch() 更新 laravel 中自定义时间戳字段的时间戳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27597465/