Eloquent Upsert vs UpdateOrCreate: Practical Demo

preview_player
Показать описание
For some Laravel forms, you need to update quite a lot of data, while also checking if it already exists. Let me show you 3 ways to do it in Eloquent.

Related links:

- - - - -
Support the channel by checking out our products:
Рекомендации по теме
Комментарии
Автор

Literally just ran into an issue with this today! Then found this video and it explained what we are doing wrong

noahbar-shain
Автор

THANK YOU! Some parts of the documentation couldn't be worse.

LuisEduardoBraschi
Автор

love how the explanation is made in details .

ramina
Автор

yeah, upsert is good ... I love upsert ... in the previous video, I'm comment suggest using upsert.

rudi
Автор

Awesome! This saved me hundreds of queries/updates. :-) Thank you!

dclindner
Автор

updateOrCreate() was my hero, but upsert() become my hero.

Irnovi
Автор

Now that's interesting, I didn't know SQL could do that on its own :D

Voltra_
Автор

$points = ($userPoints, $userId) => ['user_id' => $userId, 'points' => (int) $userPoints])->toArray();
would be better.

rahulday
Автор

Upsert doesn't work with primary keys ? I had a similar example of incrementing a field, but it would always try to insert new records

omkarjere
Автор

can you use wildcard variable inside the unique key?

elvenme
Автор

how can i get record was update or create after use upsert in laravel

chung
Автор

Thank you so much i just replacing my code. Thanks sir

anmolkaithwas
Автор

I think using unique would cause problems. Using the supplied laravel example of flights there would generally be many duplicate destinations with the departure being variable and vice versa. Looks like that requirement has since changed.

JohnvdVaart
Автор

if updateorcreate matches the date, how about it, sir, every new insert date doesn't update the data for the previous date

teknologikomunikasi
Автор

its great but how to find out how many entries have been updated and how many have been created

andrbee
Автор

to transform arrays I would use array_map()

soniablanche
Автор

hello in request validation i user it store but created_at And Update Ad Not Store AND what if i have $request->id ? how to store that id?

iamriwash
Автор

8.10.... Seems so long ago.
I don't remember when I first started using it.

Stoney_Eagle
Автор

Please am trying to upsert multiple rows but it's only inserting one row, please help

Onyecode
Автор

Nice thing! Is there a way to implement this in older versions like Laravel 7? Or to replicate this method. When you have thousand of records and you have to do a bulk update, this is a performance changer!

eusebiubalan