简体   繁体   中英

sql isnull update with laravel eloquent update

current table (TARGET)

field1   field2   field3    field4
apple1   apple2   apple3   apple4

The data I want to add (SOURCE)

SourceData = ("orange1","orange2",null,"orange4")

The result I expected after updating

field1      field2   field3   field4
orange1   orange2   apple3   orange4

I can do this with sql query, I just want to do it with laravel eloquent.

Anyone suggestion ? Thanks...

Let's say that you have find the model you want: eg: $model = App\\Target::where('field1', 'orange1');

You can use the update function:

App\Target::find(id)->update([
  'field1' => 'orange1',
  'field2' => 'orange2',
  'field4' => 'orange4'
]);

The update function updates only the specified fields.

If you want to update all rows, then the following does the trick:

App\Target::update([
  'field1' => 'orange1',
  'field2' => 'orange2',
  'field4' => 'orange4'
]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM