title: Laravel error: Trying to get property 'id' of non-object
body: <p>I have to delete product from shopping cart, but i'm stack on this below error:</p>
  <blockquote>
  <p>Trying to get property 'id' of non-object (View:
  /home/acer/test/project_basket/basket/resources/views/cart.blade.php).</p>
  </blockquote>
  <p>I tried also in different way, ... to remove sessions but i don't know seems to be a little difficult because i need to delete only one product.
  For every identical added product in shopping cart i create badge that increment by one every time.</p>
  <p>cart.blade.php</p>
  <pre><code>@foreach($products as $product)
  &lt;tr&gt;
     &lt;td&gt;&lt;img src=&quot;https://via.placeholder.com/400x200/&quot; class=&quot;img-cart&quot;&gt;&lt;/td&gt;
        &lt;td&gt;&lt;strong&gt;{{ $product['item']['title'] }}&lt;/strong&gt;&lt;/td&gt;
            &lt;td&gt;
              &lt;form class=&quot;form-inline&quot;&gt;
                   &lt;input class=&quot;form-control&quot; type=&quot;text&quot; value=&quot;{{ $product['qty'] }}&quot; style=&quot;margin-right: 20px&quot; &gt;
                    &lt;!--&lt;button rel=&quot;tooltip&quot; class=&quot;btn btn-default&quot;&gt;&lt;i class=&quot;fa fa-pencil&quot;&gt;&lt;/i&gt;&lt;/button&gt;--&gt;
                         &lt;form method=&quot;POST&quot; action=&quot;{{ route('remove.item', $product-&gt;id) }}&quot; &gt; 
                               @csrf
                               {{ method_field('DELETE') }}
                               &lt;input type=&quot;submit&quot; value=&quot;Delete&quot; onclick=&quot;return confirm('Are you sure?')&quot; class=&quot;btn btn-primary&quot; /&gt;
                                      
                         &lt;/form&gt;
                       &lt;/form&gt;
                     &lt;/td&gt;
                    &lt;td&gt;${{ $product['price'] }}&lt;/td&gt;
                &lt;/tr&gt;
  @endforeach
  </code></pre>
  <p>ProductController.php</p>
  <pre><code>&lt;?php
  
  namespace App\Http\Controllers;
  
  use App\Cart;
  use App\Product;
  
  use Illuminate\Http\Request;
  use Session;
  
  class ProductController extends Controller
  
  {
      /**
      *@return \Illuminate\Http\Response
      */
      public function index()
      {
          $products = Product::all();
          return view('home', ['products'=&gt; $products]);
      }
  
      public function getAddToCart(Request $request, $id) 
      {
          $product = Product::find($id);
          $oldCart = Session::has('cart') ? Session::get('cart') : null;
          $cart = new Cart($oldCart);
          $cart-&gt;add($product, $product-&gt;id);
  
          $request-&gt;session()-&gt;put('cart', $cart);
          return redirect()-&gt;route('home');
      }
  
      public function getCart()
      {
          if (!Session::has('cart')) {
              return view('cart');
          }
          $oldCart = Session::get('cart');
          $cart = new Cart($oldCart);
          return view('cart', ['products' =&gt; $cart-&gt;items, 'totalPrice' =&gt; $cart-&gt;totalPrice]);
      }
  
      /**
       * Remove the specified resource from storage.
       *
       * @param  \App\Product $product
       * @return \Illuminate\Http\Response
       */
      public function destroy(Product $product)
      {
          $product-&gt;delete();
  
  
          return redirect()-&gt;route('cart');
      }
  }
  </code></pre>
  <p>Routes:</p>
  <pre><code>Route::get('/add-cart/{id}', [
            'uses' =&gt; 'ProductController@getAddToCart',
            'as' =&gt; 'addToCart'
        ]);
  
  Route::get('/cart', [
            'uses' =&gt; 'ProductController@getCart',
            'as' =&gt; 'shoppingCart'
        ]);
  
  
  
  Route::get('/destroy/{id}', [
       'uses' =&gt; 'ProductController@destroy',
       'as' =&gt; 'remove.item']);
  </code></pre>
  <p>Cart.php</p>
  <pre><code>&lt;?php
  
  namespace App;
  
  class Cart 
  {
     public $items = null;
     public $totalQty = 0;
     public $totalPrice = 0;
  
     public function __construct($oldCart)
     {
       if ($oldCart) {
          $this-&gt;items = $oldCart-&gt;items;
          $this-&gt;totalQty = $oldCart-&gt;totalQty;
          $this-&gt;totalPrice = $oldCart-&gt;totalPrice;
       }
     }
  
     public function add($item, $id)
     {
       $storedItem = ['qty' =&gt; 0, 'price' =&gt; $item-&gt;price, 'item' =&gt; $item];
  
       if ($this-&gt;items ) {
  
              if (array_key_exists($id, $this-&gt;items)) {
                   $storedItem = $this-&gt;items[$id];
               }
           }
           $storedItem['qty']++;
           $storedItem['price'] = $item-&gt;price * $storedItem['qty'];
           $this-&gt;items[$id] = $storedItem;
           $this-&gt;totalQty++;
           $this-&gt;totalPrice += $item-&gt;price;
      }
  }
  </code></pre>