search

Home  >  Q&A  >  body text

Laravel Eloquent returns 0 if updated value is same as previous data in database

I have a problem in laravel 8, when I run the query below for the first time it works fine without any issues, but when I rerun it it always shows a failure message unless I add The "isprocess" value changed to be different from the previous update.

How to continue to display the success message even if the updated value remains the same?

$updateIsProses = DB::table('pricelist_naikharga')->whereDate('tanggal', '<=', '2022-05-01')->update(["isproses" => 1]);

       if($updateIsProses){
            echo outputJson(200, "BerhasilSuccess");
       }else{
            echo outputJson(500, "Fail");
       }

P粉662361740P粉662361740231 days ago382

reply all(1)I'll reply

  • P粉262926195

    P粉2629261952024-03-28 00:38:46

    The update() method in the query builder returns the number of affected rows.

    Checking for failed or successful values ​​is inaccurate because running the same query twice will cause the second query to not affect any rows and therefore return 0, which evaluates to false

    If an error occurs, an exception will be thrown

    try {
        $updatedRowCount = DB::table('pricelist_naikharga')->whereDate('tanggal', '<=', '2022-05-01')->update(["isproses" => 1]);
    catch (\Exception $e) {
        echo outputJson(500, "Fail");
    }
    
    echo outputJson(200, "BerhasilSuccess, affected Rows: ".$updatedRowCount);

    reply
    0
  • Cancelreply