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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23132758/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 09:22:29  来源:igfitidea点击:

How to update the updated_at column when the user logs in?

mysqllaraveleloquent

提问by user3416609

I'm trying to update the updated_atcolumn to the current time, each time a user logs in.

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

But I get the following error:

但我收到以下错误:

InvalidArgumentException A four digit year could not be found Data missing

InvalidArgumentException 找不到四位数的年份 数据丢失

PHP

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');
    }
}

回答by Jarek Tkaczyk

You can use the Eloquent method touch()for this:

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

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

// simply this:
$user->touch();

回答by Lee

For one I would not use the updated_at column as that's the default timestamps name.

对于其中之一,我不会使用 updated_at 列,因为这是默认的时间戳名称。

You would be better of with last_login

你最好使用 last_login

And just use the PHP date method.

并且只需使用 PHP 日期方法。

$user->updated_at = date('Y-m-d G:i:s');

Hope this helps.

希望这可以帮助。

回答by Nik K

I think you're accidentally assigning a value instead of using array syntax. eg:

我认为您不小心分配了一个值而不是使用数组语法。例如:

Model::create([
    'key'='val'
]);

instead of:

代替:

Model::create([
    'key'=>'val'
]);