我正在尝试更新表格。
我在foreach中拥有所有价值观。唯一的ID是'uuid'
但是,如果值仅已更改,我想进行更新。我尝试这样做,但是没有运气。
$results = DB::table('urls')
->where('uuid','=',$uuid)
->orWhere('id_media', '!=',$id_media)
->orWhere('region', '!=',$region)
->orWhere('page', '!=',$page)
->orWhere('audience', '!=',$audience)
->update(array(
'id_media' => $id_media,
'region'=>$region,
'page'=>$page,
'audience'=>$audience
));
下面的查询的laravel方式是什么。
update my_table set
my_col = 'newValue'
where id = 'someKey'
and my_col != 'newValue';
试试这个。在https://laravel.com/docs/5.2/queries#updates中找到更多信息。
DB::table('my_table')
->where('id', 1)
->where('my_col', '!=', 'newValue')
->update(['my_col' => 'newValue']);
在您的特定情况下,您应该使用以下命令:
DB::table('urls')
->where('uuid', '=', $uuid)
->where(function ($query) use ($id_media, $region, $page, $audience) {
$query->orWhere('id_media', '!=', $id_media)
->orWhere('region', '!=', $region)
->orWhere('page', '!=', $page)
->orWhere('audience', '!=', $audience);
})
->update([
'id_media' => $id_media,
'region' => $region,
'page' => $page,
'audience' => $audience
]);
最后一个将产生如下内容:
update my_table set
my_col = 'newValue'
where id = 'someId' and
(my_col1 != 'newValue1' or my_col2 != 'newValue2' or .. );
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句