Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Crazylife's avatar

What is the best way to write an update query if there is multiple choice selection for the data to be updated?

This is the sample query.

 $cat = DB::table('tesing')
                    ->where('id', $id)               
                    ->update(['a' => $a,'b'=>$b,'c'=>$c,'d'=>$d']);

E.g. when user is allow to choose whether want to just only update 'a' value or 'b' value and so on. How can i ensure that only update the field where selected by the user.

 $cat = DB::table('tesing')
                    ->where('id', $id)               
                    ->update(['a' => $a']);

When user select 'a' field, then only 'a' field will be updated. What way can i use?

0 likes
8 replies
hajrovica's avatar

basically you already are there, this is issue of your front end - form setup so lets say if you have check boxes as options for a, b, c, and d

$cat = DB::table('tesing')
                ->where('id', $id)               
                ->update($data);

And you will get data from your forms post data so lets say if checkbox a is selected you will have value of a in data array.

Same thing will be with text fields, changed fields will have new value and unchanged fields old value and app will perform update based on that data.

i hope this short explanation is clear ?

Crazylife's avatar

@hajrovica Sorry, i don't get it. The value from front end not to be stored, just to determine which column will be updated. After that i will only update accordingly.

Crazylife's avatar

For example, there is a remarks and quantity column. When user select only remarks will be updated, then i will perform a select query to get data from another table and update the quantity it.

Cronix's avatar

Maybe something like

$data = [];
$fields = ['a', 'b', 'c', 'd'];
foreach ($fields as $field) {
    if ($request->has($field)) {
        $data[$field] = $request->$field;
    }
}

$cat = DB::table('tesing')
                    ->where('id', $id)               
                    ->update($data);
1 like
Crazylife's avatar

@Cronix Anyhow i still need to use if-else statement to control the value to be updated right? Because the value i want to update not from the $request->$field. It is something like

foreach ($testing as $test) {
$cat = DB::table('testing')
->where('id', $id)
->update(['remarks' => $test->remarks,'quantity'=>$test->quantity]);
            }
        }

For example.

foreach ($testing as $test) {
 $data = [];
 $fields = ['remarks', 'quantity'];
 foreach ($fields as $field) {
 if ($request->has($field) =='remarks') {
      $data[$field] = $test->remarks;
                    }
 if ($request->has($field) =='quantity') {
      $data[$field] = $test->quantity;
                    }
                }
$cat = DB::table('testing')
->where('id', $id)
->update($data);
            }
        }
 

Thanks.

Snapey's avatar

as you have noted, just pass through an array of items to be updated and don't mention the columns that are to stay the same.

Crazylife's avatar

@Snapey mean that the only way to achieve is doing if-else statement to check for which column will be updated right? Correct me if i am wrong.

Snapey's avatar

If you pass through $request->all() then only fields that are sent in will be updated.

$cat = DB::table('testing')
->where('id', $id)
->update($request->all());

but, bear in mind that fields that are present, but null will be cleared in the database

Please or to participate in this conversation.