php - 使用 touch() 更新 laravel 中自定义时间戳字段的时间戳

标签 php laravel-4 orm eloquent

有没有一种方法可以使用 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/

相关文章:

PHP 邮件内容不显示在 Outlook 中

PHP "ping"防止网页超时

php - 使用 Eloquent/Raw Laravel 查询的 Group Concat

c# - Blazor依赖注入(inject)错误: An unhandled exception occurred while processing the request?

php - 尝试使用复选框创建 sql 查询

php - 什么是 PHP 函数命名和参数顺序逻辑/约定?

php - Laravel 消息包报错

php - Laravel 嵌套资源和可重用 Controller

python - 如何限制/偏移 sqlalchemy orm 关系的结果?

java - JDBC 查询缓存和预缓存