2016-11-03 68 views
0

使用laravel 5.3 IM和项目SHOOPING车 当我点击的项目的数量没有增加如何增加值时,按一下按钮laravel 5.3

产品控制研究

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

和模型车:

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; 

    } 

    } 

这是一个产品页面:

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

它应该添加项目和增量,但它只是显示项目的id与增量它。

回答

0

会话保存为闪存数据。所以,只要您推送数据,您就需要保存会话。由于你没有在你的代码中做它总是得到一个新的第一个项目被添加到它的新车,我认为你是混淆该号码与ID,因为totalQty将始终为1.

所以请执行以下操作后将购物车对象放入会话中。

​​
+0

我试过,但仍然没有工作 – ammar

+0

好让做两件事情。首先在getAddToCart中获取此三元运算符值并将其从刀片中删除。在过去,对于我来说,有时候三元操作员会陷入刀片模板中。其次,当你使用dd($ request-> session() - > all())欺骗整个会话时,请你分享结果。 OR print_r(session() - > all()); –

0

你的意思是这个

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 
} 
+0

您可以执行以下操作:$ cart-> save(),然后将其保存在会话中。我们还可以使用$ request-> session() - > push($ cart.items,'cart_items')将会议内容存储在会话中,并将它们作为数组和数组的count()进行检索,物品数量。 –

+0

它给了我一个erorr调用未定义的方法App \ Cart :: save()或使用未定义的常量项目 - 假定'项目' – ammar

+0

您是否在您的控制器中导入了使用它的Cart模型文件。 –