Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
368 views
in Technique[技术] by (71.8m points)

laravel - Check the form field if the has a data

I have this controller

public function UpdateRequest(ClientUpdate $request){
    $res = Customer::where('id', $request->id)

        
                    ->update([
                        
                        'first_name'  => $request->new_first_name,
                        'last_name'   => $request->new_last_name,
                        'birthdate'   => $request->new_birthdate,
                        'gender'      => $request->new_gender,
                        'pending_update_info' => 2,

                    ]);}

I have a form that is updating a customer what i want to do is for example the new_first_name is null and the rest of the field is have a data it will only update the field that has a data on it .

My Logic is like this:

if new_first_name is null and new_last_name is null update only the new_gender and new_birthday, same with new_first_name is null and new_last_name is null and new_gender update only the new_birthday..

I dont know how to put in code in laravel hope you'll help me


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You need to update by using array concatenate Method like following...

public function UpdateRequest(ClientUpdate $request){
    $updateItems = [];
    if($request->new_first_name){
       $updateItems['first_name'] = $request->new_first_name;
    }
    if($request->new_last_name){
       $updateItems['last_name'] = $request->new_last_name;
    }
    if($request->new_birthdate){
       $updateItems['birthdate'] = $request->new_birthdate;
    }
    if($request->new_gender){
       $updateItems['gender'] = $request->new_gender;
    }
    $updateItems['pending_update_info'] = 2;
    $res = Customer::where('id', $request->id)->update($updateItems);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...