我创建了查询以将数据插入到我的数据库表中。现在我想知道如何根据电子邮件属性更新该字段
$user = User::create([
'name' => $request->name,
'email' => $request->email,
'password' => Hash::make($request->password),
'address' => $request->address,
'phone' => $request->phone,
'user_role' => $roles,
]);
这是我的插入查询。我想要类似(更新每个列中的电子邮件=“ someemail”)
我尝试过类似的事情
$userupdate = User::fill([
'name' => $request->name,
'password' => Hash::make($request->password),
'address' => $request->address,
'phone' => $request->phone,
'user_role' => $roles,
])->where('email',$request->email);
答案 0 :(得分:2)
$userupdate = User::where('email',$request->email)->update([
'name' => $request->name,
'password' => Hash::make($request->password),
'address' => $request->address,
'phone' => $request->phone,
'user_role' => $roles,
]);
希望这对您有帮助
答案 1 :(得分:1)
您可以尝试这样的事情
$data = $request->all();
$userToUpdate = User::where('email', $request->email)->firstOrFail();
$userToUpdate->update($data);
*请注意,如果您只想更新已创建的记录,则可以执行
$userToUpdate->update($data);