0

i am attempting to make a select, if the select doesn't exist, create it, and either way, add a timestamp 'last_login'

            $user = User::find($auth['acc_id']);

            if (!$user) {
                $user = User::setRawAttributes([
                    'id' => $auth['acc_id'],
                    'username' => $auth['username'],
                    'password' => $auth['password'],
                ]);
            }
            $user->last_login = date('Y-m-d H:i:s', time());

I was expecting that to work, but i guess setRawAttributes is not what im looking for? Because i dont want to create a new user and then select it to add the last_login

3
  • Have you tried updateOrCreate method ? Commented Jun 23, 2021 at 13:20
  • @Dren not sure how that would help me? i do not want to process it at once. I want to create it, edit it and then $user->saveOrFail(); Commented Jun 23, 2021 at 13:23
  • You want to add a last_login to an existing user right? but if the user with acc_id does not exists you want to create it ? right? Commented Jun 23, 2021 at 13:27

1 Answer 1

1

This operation calls Upsert Check it in docs:

https://laravel.com/docs/8.x/eloquent#upserts

$user = User::updateOrCreate([
    'acc_id' => $auth['acc_id'],
    'username' => $auth['username'],
    'password' => $auth['password'],
], [
    'last_login' => date('Y-m-d H:i:s', time()),
]);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.