laravel - Laravel updateOrCreate 与 where 子句
问题描述
如何使用 where 子句实现updateOrCreate函数。
例如。仅当某些列包含特定值时,我才想更新字段,否则不更新。是否可以使用updateOrCreate功能?
解决方案
updateOrCreate
是 where 子句的更新:
$user = User::updateOrCreate(
[
'id' => $userId,
'type' => $userType,
],
[
'name' => $userName,
'email' => $userEmail,
]
);
此查询将使用 and 更新用户,$userName
如果$userEmail
用户存在匹配$userId
and $userType
。如果没有找到匹配的用户,则使用、$userId
和来创建一个新用户。$userType
$userName
$userEmail
因此,第一个数组是更新时必须匹配的“where-clause”,第二个数组是如果找到匹配项则应更新的值,如果没有匹配项,则在创建新用户时使用两个数组的合并成立。
推荐阅读
- c++ - 为 C++ 项目创建 makefile
- javascript - 我可以添加编辑选项并使用 javascript 赋予它功能吗
- css - 屏幕变小时gridview没有响应
- r - 如何将这些 JSON 数据转换为 R 中的数据框
- python - 如何在 OpenCV 中为这些图像获取这些 ROI
- express - 暂停所有请求并锁定 express api?
- pine-script - 在 pine-script 如何根据条件设置高点和低点?
- c - 这是使用 sleep() 的缺点还是每次迭代输出计数的问题?(子进程没有按预期执行)
- sql - spark雪花连接器sql查询sql编译报错
- vue.js - 使用 v-popover 组件定位非包装元素