在这种情况下如何获取当前的自动增量值? [英] How to get current auto incremented value in this case?

查看:59
本文介绍了在这种情况下如何获取当前的自动增量值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您将在图片中看到,时间为"left"的数据被插入到当前用户名下的每个"left"字段中.这是因为在函数logout()中,我通过用户ID查找用户.但是我想通过自动递增的'user_id'来解决用户问题.我该怎么办?

As you'll see in the picture, data with time 'left' gets inserted in every field 'left' under the name of current user. This is because in function logout() I find the user by his id. But I would like to address the user by 'user_id' which is auto increment. How could I do it?

 public function logout() {
        $id = auth()->id();
        $info = \App\UserInfo::find($id);
        $info->left = now(); 
        $info->save(); 
        auth()->logout();
        session()->forget('name');
        session()->put('left',now());
        return redirect('/');
    }

如果有一种方法可以从UserInfo对象获取此$ time值,则可以更新"left"列.

If there were a way to get this $time value from UserInfo object we could update 'left' column.

$info = \App\UserInfo::where('id', $id)
    -> where('joined', $time)->first();
    ->update(['left' => now()]);

无论如何,这是使它正常工作的线.现在,它会插入而不会覆盖其他值.

Anyway, this is the line to get it to work. Now it inserts without overriding other values.

 $user_info = \App\UserInfo::where('user_id', $id)->latest()->first();

推荐答案

通过user_id查找UserInfo

Find the UserInfo by the user_id

$info = \App\UserInfo::where('user_id', $id)->firstOrFail();

请注意,如果许多用户登录和注销,您的表将变得很大,如果您想实现消息聊天应用程序的last seen功能,则旧数据似乎不相关

Please note that your table will become huge if a lot of users login and logout, old data don't seem relevant if you want to implement the last seen feature of a messaging chat app

这篇关于在这种情况下如何获取当前的自动增量值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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