英文:
Laravel Updates existing DB values to NULL
问题
I'm trying to Update Form Values that have multiple form Fields. Everything works well only while Updated DB
laravel
updates null values in DB
, which removes existing data in the field.
My controller receives form field requests. When the record is present e.g. three and fFour
holds old values in DB
, and I try to only update one and two fields it also updates three and four to null as I only send one and two values.
public function updateSystemSettings(Request $request){
// Validate Request
$validator = Validator::make($request->all(), [
'one' => 'string|nullable',
'two' => 'string|nullable',
'three' => 'json|nullable',
'four' => 'json|nullable',
etc.. other null or holds values
]);
if($validator->fails()){
return response()->json($validator->errors()->toJson(), 400);
}
// Get First Row if Exist ELSE I will create record
$systemSettings = SystemSettings::first();
// Update
$result = array_filter(request()->all());
SystemSettings::where('id', $systemSettings->id)->update([
"one" => $result['one'],
"two" => $result['two'],
"three" => $result['three'],
other fields...
]);
// After update Send Updated Dat
$data = SystemSettings::first();
$response = json_decode($data, JSON_UNESCAPED_SLASHES);
return response()->json([
'data' => $response
]);
}
Tried almost everything, in SQL
we have something called values(?, ?, ?, ?, ? ) which only updates the fields which has values else it keeps the old DB
values as is.
英文:
I'm trying to Update Form Values that have multiple form Fields. Everything works well only while Updated DB
laravel
updates null values in DB
, which removes existing data in the field.
My controller receives form field requests. When the record is present e.g. three and fFour
holds old values in DB
, and I try to only update one and two fields it also updates three and four to null as I only send one and two values.
public function updateSystemSettings(Request $request){
// Validate Request
$validator = Validator::make($request->all(), [
'one' => 'string|nullable',
'two' => 'string|nullable',
'three' => 'json|nullable',
'four' => 'json|nullable',
etc.. other null or holds values
]);
if($validator->fails()){
return response()->json($validator->errors()->toJson(), 400);
}
// Get First Row if Exist ELSE I will create record
$systemSettings = SystemSettings::first();
// Update
$result = array_filter(request()->all());
SystemSettings::where('id', $systemSettings->id)->update([
"one" => $result['one'],
"two" => $result['two'],
"three" => $result['three'],
other fields...
]);
// After update Send Updated Dat
$data = SystemSettings::first();
$response = json_decode($data, JSON_UNESCAPED_SLASHES);
return response()->json([
'data' => $response
]);
}
Tried almost everything, in SQL
we have something called values(?, ?, ?, ?, ? ) which only updates the fields which has values else it keeps the old DB
values as is.
答案1
得分: 3
我认为你需要检查请求是否包含键(例如,three):
SystemSettings::where('id', $systemSettings->id)->update([
"one" => $result['one'] ?? $systemSettings->one,
"two" => $result['two'] ?? $systemSettings->two,
"three" => $result['three'] ?? $systemSettings->three,
其他字段...
]);
FYI:
"one" => $result['one'] ?? $systemSettings->one
等同于
"one" => $result['one'] ? $result['one'] : $systemSettings->one
英文:
I think you have to check if the request has the key (e.g. three):
SystemSettings::where('id', $systemSettings->id)->update([
"one" => $result['one'] ?? $systemSettings->one,
"two" => $result['two'] ?? $systemSettings->two,
"three" => $result['three'] ?? $systemSettings->three,
other fields...
]);
FYI:
"one" => $result['one'] ?? $systemSettings->one
is eqaul to
"one" => $result['one'] ? $result['one'] : $systemSettings->one
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论