Laravel Eloquent returns 0 if updated value is same as previous data in database
P粉662361740
P粉662361740 2024-03-27 09:07:52
0
1
388

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粉662361740
P粉662361740

reply all(1)
P粉262926195

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', 'update(["isproses" => 1]);
catch (\Exception $e) {
    echo outputJson(500, "Fail");
}

echo outputJson(200, "BerhasilSuccess, affected Rows: ".$updatedRowCount);
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template