Heebah Saleem Heebah Saleem - 1 year ago 333
PHP Question

Laravel Framework 5.4: Shopping-Cart system--add to cart button is not working

Please help me with this error.
I am working on Shopping-Cart system using Laravel 5.4. I was working on button "Add To Cart".


<?php /* |-------------------------------------------------------------------------- | Web Routes |-------------------------------------------------------------------------- | | Here is where you can register web routes for your application. These | routes are loaded by the RouteServiceProvider within a group which | contains the "web" middleware group. Now create something great! |

Route::get('/', [

'uses'=>'[email protected]', 'as'=>'product.index'


Route::get('/add/{id}', [
'uses' => '[email protected]',
'as' => 'product.addToCart' ]);

Route::group(['prefix'=>'user'], function(){ Route::group(['middleware'=>'guest'], function() {


'uses'=>'[email protected]', 'as'=>'user.signup' ]);


'uses'=>'[email protected]', 'as'=>'user.signup'



'uses'=>'[email protected]', 'as'=>'user.signin'



'uses'=>'[email protected]', 'as'=>'user.signin'

]); });

Route::group(['middleware'=>'auth'], function (){ Route::get('/profile',[ 'uses'=>'[email protected]', 'as'=>'user.profile'


Route::get('/logout',[ 'uses'=>'[email protected]', 'as'=>'user.logout'

]); });



Route::get('/home', '[email protected]');



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, $qty) {
$storedItem = ['qty'=>0, 'price'=>$this->price, 'item'=> $item]; if($this->items) { if(array_key_exists($id, $this->items)) {
$storedItem = $this->items[$id]; } }

$storedItem['qty'] += $qty; $storedItem['price'] = $item->price * $storedItem['qty']; $this->items[$id] = $storedItem; $this->totalQty += $qty; $this->totalPrice += $item->price; } }


<?php namespace App\Http\Controllers; namespace App\Http\Controllers;

use 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')?$request->session()->get('cart'):null;
$cart = new Cart($oldCart);
$cart->add($product, $product->id);
$request->session()->put('cart', $cart);
return redirect()->route('product.index');
public function getCart() {
if (!Session::has('cart')) {
return view('shop.shopping-cart');
$oldCart = Session::get('cart');
$cart = new Cart($oldCart);
return view('shop.shopping-cart', ['products' => $cart->items, 'totalPrice' => $cart->totalPrice]);
} }

Error: its been two days could not find solution. Please help!

EDIT: Here's my index.blade.php

<link rel="stylesheet" href="{{URL::to('css/app.css')}}">
Find Stuff

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

<div class="row">
@foreach($productChunk as $product)
<div class="col-sm-6 col-md-4">
<div class="i">
<div class="thumbnail">
<img src="{{$product->imagePath}}" alt="Harry Potter Books" style="max-height:200px" class="img-responsive">
<div class="caption">
<div class="clearfix">
<div style="color:purple; font-size:16px"><b>${{$product->price}}</b></div><br>
<a href=**"{{route('product.AddToCart',[ 'id' => $product->id])}}"** class="btn btn-primary" role="button">Add To Cart</a> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<a href="#" class="btn btn-success" role="button">Buy</a>


enter image description here

Answer Source

In your error image you didn't provide localhost:8000/add. Since your route accept an ID Route::get('/add/{id}' So if you didn't provide and id it will throw an error.

You need to provide an id like add/1 or add/2 etc..

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download