vendredi 23 juin 2017

How to three attributes in lists() laravel 5.2?

I want to display the record field size in the cart table according to the user's cart, but I can only display the record field quantity in the cart table.

lists() function would be useful, but it can get just two parameter. However I want to get output with three parameters, product_id, quantity and size, like this:

['product_id'=> 1, 'quantity' => 10, 'size' => 'Medium']

If possible, how to add 2 $ value in foreach. Is this wrong?

foreach ($this->lists() as $id => $quantity => $size)

this is my CartController method lists() and details():

  public function lists()
  {
      if(Auth::check()) {
          return Cart::where('user_id', Auth::user()->user_id)->lists('quantity', 'product_id');
      } else {
          return $this->request->cookie('cart');
      }
  }

  public function details()
  {
      $result = [];
      if ($this->totalProduct() > 0) {
          foreach ($this->lists() as $id => $quantity) {
              $product = Product::find($id);
              array_push($result, [
                  'product_id' => $id,
                  'detail' => $product->toArray(),
                  'quantity' => $quantity,
                  'subtotal' => $product->price * $quantity
              ]);
          }
      }
      return $result;
  }

View:

enter image description here

Aucun commentaire:

Enregistrer un commentaire