mardi 14 avril 2020

Route [/add-to-cart] not defined. (View: /projects/ProjectWebsite/resources/views/shop/index.blade.php)

Can anyone please help me, i have created a basic cart but now im trying to add sessions in order to add products to my cart however i get the error above. does anyone know how i can fix this, thanks

This is the route i set up (web.php)

Route::get('/add-to-cart/{id}', 'ProductController@getAddToCart');

this is on the controller page (productController.php)

<?php

namespace App\Http\Controllers;

use App\Cart;
use App\Product;
use Illuminate\Http\Request;

use App\Http\Requests;
use Session;



class ProductController extends Controller

{
      public function getIndex()
    {


         $products = Product::all();        
         return view('shop.index', ['products' => $products]);

    }

      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');
         dd($request->session()->get('cart'));
         return redirect()->route('shop.index');

    }


}

this is my cart page (Cart.php)

<?php

namespace App;

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

    public function __construct($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 (array_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;
}

}

this is my index page(index.php, stored in shop folder)

@section('title')

Checkouts Page

@endsection

@section('content')

@foreach($products->chunk(3) as $productChunk)

<div class="row">

@foreach($productChunk as $product)

<div class="col-sm-6 col-md-4">
<div class="thumbnail">
<img src="./images/image1.png" height="200" width="300"> 
<div class="caption">
<h3></h3>
<p class="description">  </p>

 <div class="clearfix">
 <div class="pull-left price">£</div>
 <a href="" class="btn btn-success pull-right"    role="button">Add to Basket</a>
 </div>
 </div>
  </div>
 </div>


@endforeach
</div>

@endforeach

@endsection


via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire