Home > OS >  Multiple updates in one Query
Multiple updates in one Query

Time:12-18

I ran into a "problem". I am trying to update multiple columns in one Query, with this code:

Bewerbungen::query()
                    ->where('Stellenanzeigen_ID', $bewerbung->Stellenanzeigen_ID)
                    ->where('bewerber_email', '!=', $bewerbung->bewerber_email)
                    ->update(['is_Canceled' => 1]);

Bewerbungen::query()
                    ->where('Stellenanzeigen_ID', $bewerbung->Stellenanzeigen_ID)
                    ->where('bewerber_email', '!=', $bewerbung->bewerber_email)
                    ->update(['cancel_Date' => $date]);

It works fine but it feels like it is unnecessary to have the full query two times, however when I try it like this:

Bewerbungen::query()
                    ->where('Stellenanzeigen_ID', $bewerbung->Stellenanzeigen_ID)
                    ->where('bewerber_email', '!=', $bewerbung->bewerber_email)
                    ->update(['is_Canceled' => 1])
                    ->update(['cancel_Date' => $date]);

I get the error call to a member function update() on int I also tried to include this statement directly into the update() statement:

    $date = Carbon::now()->toDateString();

I also tried this:

Bewerbungen::query()
                    ->where('Stellenanzeigen_ID', $bewerbung->Stellenanzeigen_ID)
                    ->where('bewerber_email', '!=', $bewerbung->bewerber_email)
                    ->update(['is_Canceled' => 1], ['cancel_Date' => $date]);
                

That didn't work aswell, because the second [] was greyed out. The cancel_Date has the datatype date

It works fine with two separate statements, but I was curious if I could save some code somehow.

CodePudding user response:

For update in Laravel You need to write Update values in Single array. You can not add two time update function and two different array. You can add multiple values in Single update and array.

You can check below code:

Bewerbungen::query()
        ->where('Stellenanzeigen_ID', $bewerbung->Stellenanzeigen_ID)
        ->where('bewerber_email', '!=', $bewerbung->bewerber_email)
        ->update(['is_Canceled' => 1,'cancel_Date' => $date]);
  • Related