i am stuck in when i update multiple array value then each time update same value in diffrent ids like i have three fields like email,alert_level and select in select dropdoewn depends on alert_level choose option so now i want update all three fields but in stuck
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; switch($requestData['edit_email_alert_level'][$i]){ case 'client': $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 'salescenter': $editrow['alert_level'] = "salescenter"; $editrow['client_id'] = (!empty($requestData['edit_locations'])) ? implode("," ,$requestData['edit_locations']) : NULL; $editrow['salescenter_id'] = NULL; $editrow['location_id'] = NULL; break; case 'sclocation': $editrow['alert_level'] = "sclocation"; $editrow['location_id'] = (!empty($requestData['edit_locations'])) ? implode("," ,$requestData['edit_locations']) : NULL; $editrow['client_id'] = NULL; $editrow['salescenter_id'] = NULL; break; default: break; } FraudAlert::whereIn('id',$ids)->update($editrow); } }
here is this type of error
SQLSTATE[42S22]: Column not found: 1054 Unknown column ‘0’ in ‘field list’ (SQL: update fraud_alerts
set 0
= vandan@gmail.com, updated_at
= 2020-10-15 10:20:32 where id
in (228))
here is dd of $requestData
array:6 [▼ "_token" => "sTUoBilbfwZfp0x6tSdZhB4S5YQTO7l0xlip0QzA" "clientId" => "102" "fid" => array:2 [▼ 0 => "228" 1 => "231" ] "edit_email" => array:2 [▼ 0 => "van@gmail.com" 1 => "demo@gmail.com" ] "edit_email_alert_level" => array:2 [▼ 0 => "sclocation" 1 => "client" ] "edit_locations" => array:2 [▼ 0 => "12" 1 => "3" ]
]
here is model
protected $fillable = ['email', 'phone', 'alert_level', 'client_id', 'salescenter_id', 'location_id', 'added_by', 'added_for_client', 'type'];
Advertisement
Answer
The below code update data individual entries:-
if(!empty($requestData['edit_email']) && count($requestData['edit_email']) > 0){ foreach($requestData['edit_email'] as $i => $list){ $ids = $requestData['fid'][$i]; //current id $editdata = array(); $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'])) ? implode("," ,$requestData['edit_locations']) : 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'])) ? implode("," ,$requestData['edit_locations']) : NULL; $editrow['location_id'] = NULL; break; case 'sclocation': $editrow['alert_level'] = "sclocation"; $editrow['location_id'] = (!empty($requestData['edit_locations'])) ? implode("," ,$requestData['edit_locations']) : NULL; $editrow['client_id'] = NULL; $editrow['salescenter_id'] = NULL; break; default: break; } FraudAlert::where('id',$ids)->update($editrow); } }
the problem with your code is you are update data in both entries but the data is the same for both rows.