我的查询不会更新,但是当我在 dd 中刷新时它会

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

当我 运行 正常查询时,它不会将 status_order0 更新到 1,但是当我在查询之后放置一个 dd() 函数来检查它是否会正确更新时,它会在我第一次 运行 代码时给出相同的结果,但是当我刷新页面,它将更新为 1.

我的代码通常是这样的:

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";
      }
  }
}

这是我放置 dd() 函数的地方:

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

if($orderInfo->order_status !== 1) 放在那里让我检查查询是否会被跳过。我试图改变代码的显示顺序,但没有任何区别。

为了进行批量更新,从理论上讲,您需要在模型的 $fillable 数组中定义要批量更新的所有属性。 (OrderInfo 在这种情况下)

此代码会产生大量更新,但不会影响您的 $orderInfo 模型,该模型在 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

如果您想立即对 $orderInfo 产生影响,请尝试

// 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


作为旁注,您正在进行重复 getOrderInfo() 调用

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

更新以澄清对主要评论的评论 post

truth to be told, i'm confused that this code runs at all. i meant $orderInfo is an object given you checked its property order_status but then you call where() on it as if it is a collection (or model). also its not laravel-query-builder but laravel-eloquent or just eloquent given you have detach() there.. – Bagus Tesa

如果我们深入研究 Illuminate\Database\Eloquent\Model class 就有 'magic' 方法 __call

/**
     * Handle dynamic method calls into the model.
     *
     * @param  string  $method
     * @param  array  $parameters
     * @return mixed
     */
    public function __call($method, $parameters)
    {
        if (in_array($method, ['increment', 'decrement'])) {
            return $this->$method(...$parameters);
        }

        if ($resolver = (static::$relationResolvers[get_class($this)][$method] ?? null)) {
            return $resolver($this);
        }
        // that's why OP code works
        return $this->forwardCallTo($this->newQuery(), $method, $parameters);
    }

如您所见,模型是否没有调用它的方法会将调用转发给 Builder 对象($this->newQuery() 的结果),这相当于 ModelName::query()

tbh,我同意从加载的模型调用 eloquent 有点令人沮丧,但它是 'by design'