lundi 31 juillet 2017

Trying to get property of non-object in my controller

I used two tables to retrieve data and view it on my product page. It worked properly but when I click the "add to cart" button it says 'Trying to get property of non-object'

This is my code:

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



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2w0cKfd
via IFTTT

Aucun commentaire:

Enregistrer un commentaire