6 months ago

Only update db field if input is set

Posted 6 months ago by kgp43


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()) {
            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');
        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.