سؤال

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');
    }
}
هل كانت مفيدة؟

المحلول

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

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

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

نصائح أخرى

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.

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

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

instead of:

Model::create([
    'key'=>'val'
]);
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top