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".
**Web.app(routes)**
Route::get('/', [
'uses'=>'ProductController@getIndex', 'as'=>'product.index'
]);
Route::get('/add/{id}', [ 'uses' => 'ProductController@getAddToCart', 'as' => 'product.addToCart' ]);
Route::group(['prefix'=>'user'], function(){
Route::group(['middleware'=>'guest'], function() {Route::get('/signup',[
'uses'=>'UserController@getSignup', 'as'=>'user.signup' ]);
Route::post('/signup',[
'uses'=>'UserController@postSignup', 'as'=>'user.signup'
]);
Route::get('/signin',[
'uses'=>'UserController@getSignin', 'as'=>'user.signin'
]);
Route::post('/signin',[
'uses'=>'UserController@postSignin', 'as'=>'user.signin'
]); });
Route::group(['middleware'=>'auth'], function (){ Route::get('/profile',[ 'uses'=>'UserController@getProfile', 'as'=>'user.profile'
]);
Route::get('/logout',[ 'uses'=>'UserController@getLogout', 'as'=>'user.logout'
]); });
});
Auth::routes();
Route::get('/home', 'HomeController@index');
`cart.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, $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; } }
**ProductController.php**
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!
via Heebah Saleem