简体   繁体   English

单击按钮时如何增加数值laravel 5.3

[英]How to increment value when the button click laravel 5.3

im using laravel 5.3 and the project is shooping cart When I click the items the quantity is not increment 即时通讯使用laravel 5.3,该项目是购物车当我单击项目时数量不增加

the Product Controll 产品控制

public function getAddToCart(Request $request, $id)
{
  $product  =Product::find($id);
  $oldcart = Session::has('cart') ? Session::get('cart') : null;
  $cart = new Cart($oldcart);
  $cart->add($product , $product->id);

  $request->session()->put('cart',$cart);
// TO show it  dd($request->Session()->get('cart'));
  return redirect()->route('product.index');
}

and the model Cart : 和模型车:

 class Cart
 {
 public $items = null;
public $totalQty = 0;
public $totalPrice = 0;

public function __consruct($oldCart){
  if($oldCart){
    $this->$items = $oldCart->items;
    $this->$totalQty  = $oldCart->totalQty;
    $this ->$totalPrice = $oldCart->totalPrice;
  }

}
   public function add($item,$id){
    $storedItem = ['qty' => 0,'price' => $item->price,'item' => $item];
    if ($this->items)
    {
      if(arrary_Key_exists($id,$this->items))
      {
        $storedItem = $this->items[$id];
      }
    }
      $storedItem['qty']++;
      $storedItem['price'] = $item->price * $storedItem['qty'];
      $this->items[$id] = $storedItem;
      $this->totalQty++;
      $this->totalPrice += $item->price;

  }

  }

And this is a Product page: 这是产品页面:

  <a href="#"> <i class="fa fa-shopping-cart" aria-hidden="true"></i> Shopping Cart
        <span class="badge">{{ Session::has('cart') ? Session::get('cart')->totalQty : '' }}</span>
      </a>

it should add the item and increment but it's just show the id of item with out increment it . 它应该添加项目并增加,但只是显示项目的ID而没有增加它。

Session is saved as a flash data. 会话保存为Flash数据。 So, you need to save the session whenever you push the data. 因此,每当您推送数据时,都需要保存会话。 Since you are not doing it in your code always gets a fresh new cart with 1st item being added to it and i think you are confusing that number with the ID since totalQty will always be 1. 由于您未在代码中执行此操作,因此总会得到一个新的购物车,其中添加了第一个项目,我认为您正在将该数字与ID混淆,因为totalQty始终为1。

So please do the following after putting the cart object in session. 因此,在将购物车对象放入会话后,请执行以下操作。

$request->session()->save();

you mean this 你是这个意思

    array:4 [▼
 "_token" => "NaciZTnMzNzqB02Nvr7RtXJj7c5NtsUF339522l2"
 "_previous" => array:1 [▼
   "url" => "http://localhost:8000"
    ]
  "_flash" => array:2 [▼
"old" => []
"new" => []
]  

Cart {#163 ▼
+items: array:1 [▼
1 => array:3 [▼
  "qty" => 1
  "price" => 20
  "item" => Product {#172 ▼
    #fillable: array:4 [▼
      0 => "imgaePath"
      1 => "title"
      2 => "description"
      3 => "price"
    ]
    #connection: null
    #table: null
    #primaryKey: "id"
    #keyType: "int"
    #perPage: 15
    +incrementing: true
    +timestamps: true
    #attributes: array:7 [▼
      "id" => 1
      "created_at" => null
      "updated_at" => null
      "imagePath" => "http://www.abebooks.com/images/books/harry-potter/deathly-hallows.jpg"
      "title" => "hurry potter"
      "description" => "the best book ever"
      "price" => 20
    ]
    #original: array:7 [▼
      "id" => 1
      "created_at" => null
      "updated_at" => null
      "imagePath" => "http://www.abebooks.com/images/books/harry-potter/deathly-hallows.jpg"
      "title" => "hurry potter"
      "description" => "the best book ever"
      "price" => 20
    ]
    #relations: []
    #hidden: []
    #visible: []
    #appends: []
    #guarded: array:1 [▼
      0 => "*"
    ]
    #dates: []
    #dateFormat: null
    #casts: []
    #touches: []
    #observables: []
    #with: []
    +exists: true
    +wasRecentlyCreated: false
   }
]
]
+totalQty: 1
+totalPrice: 20
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM