Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the differences between updateOrCreate() and updateOrInsert() in Laravel

Tags:

Both methods seem to create a new record if not existed or update a record with provided data. Whats the difference between the two?

like image 630
Daniel Avatar asked Jun 07 '17 08:06

Daniel


1 Answers

updateOrCreate is method of Eloquent Builder and updateOrInsert is method of Query Builder.

updateOrCreate returns model, whereas updateOrInsert returns boolean


Signatures from Laravel code:

updateOrCreate

/**  * Create or update a record matching the attributes, and fill it with values.  *  * @param  array  $attributes  * @param  array  $values  * @return \Illuminate\Database\Eloquent\Model|static  */ public function updateOrCreate(array $attributes, array $values = []) 

updateOrInsert

/**  * Insert or update a record matching the attributes, and fill it with values.  *  * @param  array  $attributes  * @param  array  $values  * @return bool  */ public function updateOrInsert(array $attributes, array $values = []) 
like image 55
Kyslik Avatar answered Oct 29 '22 08:10

Kyslik