如何使用firstOrCreate函数检查两列?我需要像where('email', $request->email)->orWhere('personal_code', $request->personal_code)
这样工作,而不是像where('email', $request->email)->where('personal_code', $request->personal_code)
$user = User::firstOrCreate(
[
'email' => $request->email,
'personal_code' => $request->personal_code,
],
[
'name' => $request->tenant,
'phone' => $request->phone,
'address' => $request->address
]
);
谢谢您的帮助!
答案 0 :(得分:2)
您将无法在or
方法中运行firstOrCreate()
。
要实现此目的,您需要使用自定义功能进行创建:
$user = User::where('email', $request->email)
->orWhere('personal_code', $request->personal_code);
$user = $user->exists()
? $user->first()
: User::create($request->only(['email', 'personal_code', 'name', 'phone', 'address']));
// or $request->expect('_token') if all data passed via form.