본문 바로가기
MySql

MySQL 사용자가 로그인 할 때 updated_at 열을 업데이트하는 방법은 무엇입니까?

by 베이스 공부 2020. 12. 6.
반응형

사용자가 로그인 할 때마다 updated_at 열을 현재 시간으로 업데이트하려고합니다.

하지만 다음과 같은 오류가 발생합니다.

InvalidArgumentException 4 자리 연도를 찾을 수 없습니다. 데이터가 없습니다.

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

 

해결 방법

 

이를 위해 Eloquent 메서드 touch () 를 사용할 수 있습니다.

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

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

 

참조 페이지 https://stackoverflow.com/questions/23132758

 

 

반응형

댓글