kgp43
6 months ago

Only update db field if input is set

Posted 6 months ago by kgp43

Hi,

I have this update:

    public function update(Request $request, $id)
    {
        
        $validator = Validator::make($request->all(), [
            'inlet_id' => 'required|numeric',
            'outlet_id' => 'required|numeric',
        ]);
        
        if ($validator->fails()) {
            notify()->error('Error');
            return back();
        }
        
        $conf = Configurator::find($id);
        
        # Check if Inlet ID is set
        if(is_numeric($request->get('inlet_id'))) {
            $conf->inlet_id = $request->get('inlet_id');
        }
        
        # Check if Outlet ID is set
        if(is_numeric($request->get('outlet_id'))) {
            $conf->outlet_id = $request->get('outlet_id');
        }
        
        $conf->save();
        
        notify()->success('Change saved');
        return back();
    }

The input is updated from modals, so one at a time. Thats why I got the if statements.

The problem is the validation. It fails, because the input fields are never set at the same time.

Any idea how to fix this?

Please sign in or create an account to participate in this conversation.