Laravel 如何仅在值更改时更新 mysql 中的值
Laravel how to update value in mysql only if value is changed
我是 Laravel 的新手。我要做的就是如下:
我的表单中有一些字段,例如 TITLE、DESCRIPTION。
TITLE 字段在数据库中是唯一的。
这就是我更新价值观的方式。
$product = Product::find($id);
$product->title = $request->title;
$product->description = $request->description;
$product->save();
但这会出错(该值已经存在),因为我的 TITLE 字段是唯一的。
我只想更新我的 TITLE 仅当 TITLE 值更改时,否则更新相同的值但更新其他字段。谢谢
是这样的吗?
$product = Product::find($id);
if ($product->title == $request->title) {
$product->description = $request->description;
} else {
$product->title = $request->title;
}
$product->save();
我没有看到你的任何数据库结构或表单代码,只有上面你所说的一小段。
你可以这样做...
$checkTitle = Product::where('title', '=', $request->input('title')->first();
你也可以做...
$record = Product::find($id)
if($request->input('title') === $record->title){
// Record with the $id has the same title as you are posting in the request.
}else{
// Title for $id lets say id number 3, Is not the same as the form request you are sending so now Perform insert
}
if(count($checkTitle) >= 1){
// Means the title exists and do what ever you want...
// Perform update
}else{
// Perform insert
}
抱歉,简短而甜蜜,我要去办公室了,我想这可能对现在 post 有帮助。让我知道我是否在错误的轨道上,我会尽我所能提供帮助。
您发布的代码完全(如果间接地)符合您的要求。您的问题是另一个问题:您分配的标题已在其他地方使用,从而违反了唯一性。
您必须确认 新 标题尚未使用。
$duplicate = Product::where('title', '=', $request->title)->where('id', '!=', $id)->first();
此时不清楚你想做什么:
a) 是否要使用 而不是 $id
指示的记录的 ?
b) 这是一个错误吗?
c) 是否应该更新 $id
记录,只保留标题?
你可以做任何这些事情;你不能做的是"update my TITLE only if the TITLE value is changed",因为更改的值已经在其他地方使用了。
您可以使用if
语句来检查天气列值是否与原始值相同?
$currentProduct = Product::find($id);
//Check and update if title not same
if($currentProduct->title == $request->title){
$currentProduct->description = $request->description;
}
//Update title and description...
else {
$currentProduct->title = $request->title;
$currentProduct->description = $request->description;
}
$currentProduct->save();
您应该查看 Laravel 中的 wasChanged() 函数。
我是 Laravel 的新手。我要做的就是如下:
我的表单中有一些字段,例如 TITLE、DESCRIPTION。
TITLE 字段在数据库中是唯一的。
这就是我更新价值观的方式。
$product = Product::find($id);
$product->title = $request->title;
$product->description = $request->description;
$product->save();
但这会出错(该值已经存在),因为我的 TITLE 字段是唯一的。
我只想更新我的 TITLE 仅当 TITLE 值更改时,否则更新相同的值但更新其他字段。谢谢
是这样的吗?
$product = Product::find($id);
if ($product->title == $request->title) {
$product->description = $request->description;
} else {
$product->title = $request->title;
}
$product->save();
我没有看到你的任何数据库结构或表单代码,只有上面你所说的一小段。
你可以这样做...
$checkTitle = Product::where('title', '=', $request->input('title')->first();
你也可以做...
$record = Product::find($id)
if($request->input('title') === $record->title){
// Record with the $id has the same title as you are posting in the request.
}else{
// Title for $id lets say id number 3, Is not the same as the form request you are sending so now Perform insert
}
if(count($checkTitle) >= 1){
// Means the title exists and do what ever you want...
// Perform update
}else{
// Perform insert
}
抱歉,简短而甜蜜,我要去办公室了,我想这可能对现在 post 有帮助。让我知道我是否在错误的轨道上,我会尽我所能提供帮助。
您发布的代码完全(如果间接地)符合您的要求。您的问题是另一个问题:您分配的标题已在其他地方使用,从而违反了唯一性。
您必须确认 新 标题尚未使用。
$duplicate = Product::where('title', '=', $request->title)->where('id', '!=', $id)->first();
此时不清楚你想做什么:
a) 是否要使用 而不是 $id
指示的记录的 ?
b) 这是一个错误吗?
c) 是否应该更新 $id
记录,只保留标题?
你可以做任何这些事情;你不能做的是"update my TITLE only if the TITLE value is changed",因为更改的值已经在其他地方使用了。
您可以使用if
语句来检查天气列值是否与原始值相同?
$currentProduct = Product::find($id);
//Check and update if title not same
if($currentProduct->title == $request->title){
$currentProduct->description = $request->description;
}
//Update title and description...
else {
$currentProduct->title = $request->title;
$currentProduct->description = $request->description;
}
$currentProduct->save();
您应该查看 Laravel 中的 wasChanged() 函数。