web-dev-qa-db-ja.com

ユーザーがログインしたときにupdated_at列を更新するにはどうすればよいですか?

ユーザーがログインするたびに、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');
    }
}
23
user3416609

これにはEloquentメソッドtouch()を使用できます。

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

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

1つは、updated_at列はデフォルトのタイムスタンプ名であるため、使用しません。

あなたはlast_loginのほうがいいでしょう

PHP日付メソッドを使用するだけです。

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

お役に立てれば。

8
Lee

配列構文を使用する代わりに、誤って値を割り当てていると思います。例えば:

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

の代わりに:

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