I want to add multiple where clause and multiple field to update in a single query. The code below dosen't return error but dosen't do anything :( pls help
$query = DB::table($table);
foreach($where as $key => $value ){
if($key != 0){
$query -> where( $key , '=' , $value);
}
}
foreach($field as $key => $value ){
if($key != 0){
$query -> update([$key => $value]);
}
}
$result2 = $query->get();
you can not use where inside the loop. There will be only one where clause. you can use "orWhere" along with where clause. For more details please refer this
This isn't working because you are not storing the new query builder constraint the a variable and when using get()
just use your original value of DB::table($table)
.
Do this instead:
$query = DB::table($table);
foreach($where as $key => $value ){
if($key != 0){
$query = $query -> where( $key , '=' , $value);
}
}
foreach($field as $key => $value ){
if($key != 0){
$query = $query -> update([$key => $value]);
}
}
$result2 = $query->get();
Partial solution: with this code i can update multiple field but need multiple query, even if it is automatically. where clauses must be declared at all :( ANOTHER PROBLEM is that if i don't comment the if line it create an error but if i leave it there, one update is to attribute 0 :(
foreach($field as $key => $value ){
//if($key != 0){
DB::table($table)
->where('iduser', '=', $id)
->where('iddeal', '=', $id_deal)
->where('idplan', '=', $id_plan)
->where('idtime', '=', $id_time)
->update(array($key => $value));
//}
}