如何在用户登录时更新updated_at列?

use*_*609 23 mysql laravel eloquent

updated_at每次用户登录时,我都会尝试将列更新为当前时间.

但是我收到以下错误:

InvalidArgumentException无法找到四位数年份数据缺失

PHP

$input = Input::all();
$remember = (Input::has('remember')) ? true : false;

$auth = Auth::attempt([
    'username' => $input['username'],
    'password' => $input['password'],
    'active' => 1
    ],$remember
);

if ($auth) 
{
    $user = Auth::user();
    $user->updated_at = DB::raw('NOW()');
    $user->save();

    if ($user->userType==1) 
    {
        return Redirect::intended('admin');
    }
    elseif ($user->userType==2)
    {
        return Redirect::intended('corporate');
    }
    elseif ($user->userType==3) 
    {
        return Redirect::intended('trainer');
    }
    elseif ($user->userType==4) 
    {
        return Redirect::intended('user');
    }
}
Run Code Online (Sandbox Code Playgroud)

Jar*_*zyk 69

您可以使用Eloquent方法touch():

//instead of
$user->updated_at = DB::raw('NOW()');
$user->save();

// simply this:
$user->touch();
Run Code Online (Sandbox Code Playgroud)


Lee*_*Lee 8

首先,我不会使用updated_at列作为默认时间戳名称.

使用last_login会更好

只需使用PHP日期方法即可.

$user->updated_at = date('Y-m-d G:i:s');
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助.

  • 根据您的应用程序,我看不出为什么使用updated_at是不可行的。那不是updated_at的功能吗? (2认同)