Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

My query will not update, but when i refresh in dd it will

So I have a problem with my laravel query. I have been searching around, but I couldn’t quite find what i was looking for to solve this problem.

When i run the query normally it won’t update the status_order from a 0 to an 1, but when I put a dd() function after the query to check if it will update properly, it will give the same result the first time i run the code, but when I refresh the page it will update to a 1.

Here’s how my code usually looks:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

public function payment(Request $request){
      $total = 0;
      $orderInfo = $this->getOrderInfo();
      $json2 = array();
        foreach($this->getOrderInfo()->products as $product){
          $total += ($product->price * $product->pivot->order_quantity);
        }
      if(Customer::find(Auth::user()->id)->balance >= $total && $orderInfo !== null){
        if($orderInfo->order_status !== 1){
        $orderInfo->where('customer_id', Auth::user()->id)
                  ->where('order_status', 0)
                  ->update(['order_status' => 1]);
      }
        foreach($orderInfo->products as $product){
          $json = array('order_id' => $product->pivot->order_id,
                        'product_id' => $product->pivot->product_id,
                        'product_name' => $product->name,
                        'price' => $product->price,
                        'quantity' => $product->pivot->order_quantity);
          array_push($json2, $json);
        }
        Customer::where('id', Auth::user()->id)->decrement('balance', $total);
        array_push($json2, array('order_status' => $orderInfo->order_status));
        $productInfo = json_encode($json2, JSON_PRETTY_PRINT);
        OrderHistory::create([
          'customer_id' => Auth::user()->id,
          'orderInfo' => $productInfo
        ]);
        $orderInfo->products()
        ->detach();
        $orderInfo->delete();
        return back();
      }else{
        return "Not enough balance";
      }
  }
}

Here’s where I put my dd() function:

  if($orderInfo->order_status !== 1){
        $orderInfo->where('customer_id', Auth::user()->id)
                  ->where('order_status', 0)
                  ->update(['order_status' => 1]);
          dd($orderInfo->where('customer_id', Auth::user()->id)->where('order_status', 0));
      }

The if($orderInfo->order_status !== 1) is put in there for me to check if the query would get skipped at all. I have tried to alter the order the code is presented in, but it didn’t make any difference.

I am fairly new to Laravel, so I might not pick up on really obvious errors within my code, but I have been struggling with why it won’t execute the query, so any help would be appreciated.

>Solution :

this code produce mass update but doesn’t affect your $orderInfo model which is loaded when it had order_status == 0

$orderInfo = $this->getOrderInfo();
// ...
$orderInfo->where('customer_id', Auth::user()->id)
                  ->where('order_status', 0)
                  ->update(['order_status' => 1]);
// in database data was updated, but $orderInfo is already in memory, so
// $orderInfo->order_status == 0

in case you want to get immediately impact on $orderInfo try

// if you order can have only one info
$orderInfo->order_status = 1;
$orderInfo->save();

// if order can have multiple info models
$orderInfo->newQuery()->where('customer_id', Auth::user()->id)
                  ->where('order_status', 0)
                  ->update(['order_status' => 1]);
$orderInfo = $orderInfo->fresh();

docs about fresh() method


as a sidenote here you’re doing duplicate getOrderInfo() call

  $orderInfo = $this->getOrderInfo();
  $json2 = array();
  //foreach($this->getOrderInfo()->products as $product) {
  foreach($orderInfo->products as $product) {
    $total += ($product->price * $product->pivot->order_quantity);
  }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading