0

I am trying to update the table.

I have all values in foreach. The unique id is 'uuid'

But I would like to update if the value has been changed only. I tried to do this but no luck.

$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
                    ));

what would be the laravel way for below query.

update my_table set
my_col = 'newValue'
where id = 'someKey'
and my_col != 'newValue';

1 Answer 1

1

try this. find more in https://laravel.com/docs/5.2/queries#updates.

DB::table('my_table')
    ->where('id', 1)
    ->where('my_col', '!=', 'newValue')
    ->update(['my_col' => 'newValue']);

In your particular case, you should use this:

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
        ]);

The last would produce something like this:

update my_table set
    my_col = 'newValue'
where id = 'someId' and 
    (my_col1 != 'newValue1' or my_col2 != 'newValue2' or .. );
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @Ignacio Fassini , This is exactly what i want

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.