如何在 laravel 5 中使用具有多个条件的 where 编写查询

how to write query with where having multiple condition in laravel 5

我正在尝试更新 table。

我在 foreach 中拥有所有值。唯一标识为 '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 .. );