I use two table to retrieve data and view it to my product page it work properly but wet I click the add to cart button it say 'Trying to get property of non-object'
this is my code plss hepl me how to rewrite this
public function getIndex()
 {
     //display all products
     $products = DB::table('products')
         ->join('categories', 'products.category_id', '=' ,'categories.id')
         ->select('products.*', 'categories.*')
         ->paginate(6);
     //display all mobiles category
     $mobiles = DB::table('products')
         ->join('categories', 'products.category_id', '=' ,'categories.id')
         ->select('products.*', 'categories.*')
         ->where('category_id', 1)
         ->paginate(6);
     return view('shop.index', ['products' => $products, 'mobiles' => $mobiles]);
 }
//add to cart controller 
 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);
    return redirect()->route('product.index');
 }
via Chebli Mohamed
 
Aucun commentaire:
Enregistrer un commentaire