Laravel 更新现有数据库值为 NULL

huangapple go评论54阅读模式
英文:

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

huangapple
  • 本文由 发表于 2023年5月21日 15:11:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76298708.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定