jeudi 28 février 2019

How to leave the data in database if edit form field is left empty

I getting many inputs from a user in create form(>50 fields) where I would like to edit those details in edit form.

I have made all the fields nullable so that while editing, the fields are left alone,

Controller:

public function update(Request $request, $id)
    {
        $engineers = Engineers::findOrFail($id);
        $engineers->input1 = $request->input('input1');
        $engineers->input2 = $request->input('input2');
        $engineers->input3 = $request->input('input3');
        $engineers->input4 = $request->input('input4');
        $engineers->save();
    }

When I try to edit input2 leaving everything blank, all the other fields are blank in the database.

Another option I found out was

public function update(Request $request, $id)
{
    $engineers = Engineers::findOrFail($id);
    if($request->input('input1')){
        $engineers->input1 = $request->input('input1');
    }
    if($request->input('input2')){
        $engineers->input2 = $request->input('input2');
    } 
    if($request->input('input2')){
        $engineers->input2 = $request->input('input2');
    }
     if($request->input('input2')){
        $engineers->input2 = $request->input('input2');
    }

    $engineers->save();
}

By doing the above, The corresponding record changes and all the other fields are left intact.

I also noticed that empty edit form can be submitted.

Are there any other better approach to this?



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2T8kD1t
via IFTTT

Aucun commentaire:

Enregistrer un commentaire