vandan's avatar
Level 13

how to array to update multiple rows

hii i m stuck in array through update my records i have array set in which i update multiple rows so how can i do it here is my code

if(!empty($requestData['edit_email']) && count($requestData['edit_email']) > 0){
            foreach($requestData['edit_email'] as $i => $list){
                $ids = array();
                $editdata = array();
                array_push($ids,$requestData['fid']);                    
                $editrow['email']=$requestData['edit_email'][$i];
                $editrow['phone'] = NULL;
                // dd($requestData['edit_email_alert_level']);
                switch($requestData['edit_email_alert_level'][$i]){
                    case "0":
                        $editrow['alert_level'] = "client";
                        $editrow['client_id'] = (!empty($requestData['edit_locations'])) ? implode("," ,$requestData['edit_locations']) : NULL;
                        $editrow['salescenter_id'] = NULL;
                        $editrow['location_id'] = NULL;
                        break;
                    case "1":
                        $editrow['alert_level'] = "salescenter";
                        $editrow['salescenter_id'] = (!empty($requestData['edit_locations'])) ? implode("," ,$requestData['edit_locations']) : NULL;
                        $editrow['client_id'] = NULL;
                        $editrow['location_id'] = NULL;
                        break;
                    case "2":
                        $editrow['alert_level'] = "sclocation";
                        $editrow['location_id'] = (!empty($requestData['edit_locations'])) ? implode("," ,$requestData['edit_locations']) : NULL;                            
                        $editrow['salescenter_id'] = NULL;
                        $editrow['client_id'] = NULL;
                        break;
                    default:
                        break;
                }                    
            } 
            array_push($editdata,$editrow);
            FraudAlert::where('id', $ids)->update($editdata);
            // dd($ids);
            // dd($requestData['fid']);               
        }

when i update record then error SQLSTATE[42S22]: Column not found: 1054 Unknown column '0' in 'field list' (SQL: update fraud_alerts set 0 = [email protected], updated_at = 2020-10-15 05:23:31 where id = 222)

0 likes
11 replies
SilenceBringer's avatar

Hi @van1310 If you have specific $editrow for every specific $id - you can't update multiple rows at once. Only if you have the same $editrow for a bunch of ids

1 like
SilenceBringer's avatar

@van1310 if you have different data for every id - you need to update it one by one. if you have the same data for some ids, you can use it like so

            FraudAlert::where('id', $ids)->update($editrow);
1 like
vandan's avatar
Level 13

@silencebringer @sinnbeck i use whereIn() but in case when i update data then old data save and new data also add so not remove old and replace new data

Sinnbeck's avatar

Can you give an example of where it does not work?

1 like
vandan's avatar
Level 13

@sinnbeck i have field like email, alert_level, select

in which its alert_level fields like client/salescenter/location and select field like depend on alert_level

when i update all three fields then its not working like its multiple times

vandan's avatar
Level 13

@sinnbeck @silencebringer

array:6 [▼
      "_token" => "sTUoBilbfwZfp0x6tSdZhB4S5YQTO7l0xlip0QzA"
     "clientId" => "102"
     "fid" => array:2 [▼
          0 => "228"
          1 => "231"
     ]
     "edit_email" => array:2 [▼
         0 => "[email protected]"
         1 => "[email protected]"
   ]
   "edit_email_alert_level" => array:2 [▼
      0 => "sclocation"
      1 => "client"
   ]
  "edit_locations" => array:2 [▼
     0 => "12"
     1 => "3"
  ]

]

this type of array i need to update for each id so how can i do it

SilenceBringer's avatar

@van1310 one by one only

foreach (request('fid') as $index => $id) {
    FraudAlert::where('id', $id)->update([
        'email' => request('edit_email.' . $index),
        // other fields
    ]);
}
vandan's avatar
Level 13

i do it my self resolved issue thank for support @silencebringer @sinnbeck

 elseif(!empty($requestData['edit_email']) && count($requestData['edit_email']) > 0){
            foreach($requestData['edit_email'] as $i => $list){                    
                $ids = array();
                $ids = $requestData['fid-'.($i+1)]; //current id
                $editrow['email']=$requestData['edit_email'][$i];
                $editrow['phone'] = NULL;
                switch($requestData['edit_email_alert_level'][$i]){
                    case 'client':
                        $editrow['alert_level'] = "client";
                        $editrow['client_id'] = (!empty($requestData['edit_locations-'.($i+1)])) ? implode("," ,$requestData['edit_locations-'.($i+1)]) : NULL;
                        $editrow['salescenter_id'] = NULL;
                        $editrow['location_id'] = NULL; 
                        break;
                    case 'salescenter':
                        $editrow['alert_level'] = "salescenter";
                        $editrow['client_id'] = NULL;
                        $editrow['salescenter_id'] = (!empty($requestData['edit_locations-'.($i+1)])) ? implode("," ,$requestData['edit_locations-'.($i+1)]) : NULL;
                        $editrow['location_id'] = NULL;
                        break;
                    case 'sclocation':
                        $editrow['alert_level'] = "sclocation";
                        $editrow['location_id'] = (!empty($requestData['edit_locations-'.($i+1)])) ? implode("," ,$requestData['edit_locations-'.($i+1)]) : NULL; 
                        $editrow['client_id'] = NULL;
                        $editrow['salescenter_id'] = NULL;                            
                        break;
                    default:
                        break;
                }
                FraudAlert::where('id',$ids)->update($editrow);
            }              
        }

Please or to participate in this conversation.