updateOrCreate() 和 updateOrInsert() 兩個方法都是用來儲存資料的時候方便操作“ 存在即更新,反之則建立 ”的
updateOrCreate 方法使用的是 Eloquent ORM 操作的資料庫(支援自動新增建立和更新時間),updateOrInsert 方法使用的是查詢構造器(不可以自動新增建立和更新時間)
updateOrCreate 返回值是\Illuminate\Database\Eloquent\Model, updateOrInsert 返回的是 bool。可以看兩個方法的原始碼註釋部分的 @return
下面是updateOrCreate文件說明和原始碼

$flight = App\Flight::updateOrCreate(
['departure' => 'Oakland', 'destination' => 'San Diego'],
['price' => 99]
);
updateOrCreate 原始碼部分:
public function updateOrCreate(array $attributes, array $values = [])
{
return tap($this->firstOrNew($attributes), function ($instance) use ($values) {
$instance->fill($values)->save();
});
}
updateOrInsert 原始碼部分:
public function updateOrInsert(array $attributes, array $values = [])
{
if (! $this->where($attributes)->exists()) {
return $this->insert(array_merge($attributes, $values));
}
return (bool) $this->take(1)->update($values);
}