使用touch()更新laravel中自定义时间戳字段的时间戳 [英] using touch() to update timestamp of custom timestamp field in laravel

查看:617
本文介绍了使用touch()更新laravel中自定义时间戳字段的时间戳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用touch()更新表中is_online字段的时间戳,而不是更新 laravel Eloquent ORM

Is there a way to use touch() for updating timestamp of is_online field in table instead of updating created_at field in laravel Eloquent ORM

目前我正在使用

User::where('id',$senderId )->update(array('is_online' => date('Y-m-d H:i:s')));

推荐答案

不,touch方法不是用于更新除内置时间戳记之外的任何东西,但是如果您愿意,可以在用户模型中编写自己的函数想要.像这样

No, the touch method isn't written for updating anything other than the built in timestamps, but you could write your own function in your User Model, if you want to. Something like this

class User extends Eloquent implements UserInterface, RemindableInterface {

    public function touchOnline()
    {
        $this->is_online = $this->freshTimestamp();
        return $this->save();
    }
}

然后将您的旧代码替换为

and then do replace your old code with

User::find($senderId)->touchOnline();

多几行代码,但可读性更高.

A few more lines of code, but maybe a slight bit more readable.

您可以查找背后的代码如果您好奇的话,请点击此处的触摸功能.

这篇关于使用touch()更新laravel中自定义时间戳字段的时间戳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆