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

vinoth06's avatar

Update Value based on the Input

Hi,

Am having a table with three column,

| condition | col1 | col2 |
|-------------|-------------|-------------|
|A |yes |no  |
|B |no  |yes |
|B |no  |yes |
|A |yes |no  |

The input will be either A or B. If the input is A then we need to update the column 'col1' to yes and 'col2' to no and vice-versa.

At present am using two queries to perform this operation like,

tablename::where($request->input,'A')->update(['col1'=>'yes','col2'=>'no']);

tablename::where($request->input,'B')->update(['col2'=>'yes','col1'=>'no']);
0 likes
3 replies
bobbybouwmann's avatar

I really don't understand your question... What do you want to achieve? Something like a switch?

vinoth06's avatar

HI @bobbybouwmann ,

Actually based on the input either A or B, I need to update the column based on the input A or B to col1 or col2 respectively.

Example.

  1. User gives the input as A then I need to update the col1 to Yes and col2 to No
  2. Suppose User give the input as B then I need to update the col1 to No and col2 to Yes

Please let me know if am not clear..

bobbybouwmann's avatar

Well your query is unclear. You are doing a where check on your model based on the input, but that seems weird to me. I would do something like this

switch($request->get('field')) {
    case 'A':
        $updates = ['col1'=>'yes','col2'=>'no'];
        break;
    case 'B':
        $updates = ['col1'=>'no','col2'=>'yes'];
        break;  
}

// Do your query here
Model::update($updates);

Please or to participate in this conversation.