-2
$variable = array("apple" , "orange" , null, "apple2");
DB::statement("UPDATE table1 
                            SET 
                            field1= IsNull($variable[0],field1), 
                            field2= IsNull($variable[2],field2), 
                    where = someconditional")

I want if field is null in data array do not update but only that field. other fields (if not null) do it update How can I write this code in laravel ?

Robin Vlaar
  • 27
  • 1
  • 7

1 Answers1

0

Try this

$variable = array("apple" , "orange");
DB::table('table1')->where('some_field', $condition)->update(['field1' => $variable[0], 'field2' => $variable[1]]);
CupOfSalt
  • 55
  • 8
  • Thanks but that's not what I want if field is null in data array do not update but only that field. other fields (if not null) do it update.. (I edited the question for it to be more understandable) – Robin Vlaar May 07 '18 at 00:37