Skip to content
Discussion options

You must be logged in to vote

I think you can also do it using eloquent like this:

User::query()->updateOrInsert(
        attributes: ['id' => $request->id],
        values: values: fn ($exists) => $exists // I can pass a closure that returns different data, unlike above
            ? [ 'UserName' => "{$request->surname}, {$request->name}" ]
            : [
                'UserId' => $request->id
                'UserName' => "{$request->surname}, {$request->name}"
            ]
    );

But you need to consider when using updateOrInsert directly with the QueryBuilder syntax, it won’t trigger Eloquent events.

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@Manueljlin
Comment options

Answer selected by Manueljlin
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Ideas
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #51976 on July 02, 2024 05:19.