Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

But I get the following error:

InvalidArgumentException A four digit year could not be found Data missing

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');
    }
}
like image 657
Xolmer Avatar asked Apr 17 '14 12:04

Xolmer


2 Answers

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

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

// simply this:
$user->touch();
like image 50
Jarek Tkaczyk Avatar answered Nov 09 '22 05:11

Jarek Tkaczyk


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

You would be better of with last_login

And just use the PHP date method.

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

Hope this helps.

like image 9
Lee Avatar answered Nov 09 '22 05:11

Lee