title: Showing categories and lowest price with Laravel
body: <p>Trying to show all categories from database and based of products in category to show lowest price. So what I have so far is:
  Model <code>Categories.php</code></p>
  
  <pre><code>class Categories extends Eloquent {
      protected $table = 'category';
      protected $primaryKey = 'category_id';
  
      public $timestamps = false;
  
      public function products()
      {
          return $this-&gt;hasMany('Product', 'category_id');
          //return $this-&gt;belongsToMany('Product', 'category_id');
      }
  }
  </code></pre>
  
  <p>This is <code>Product.php</code> Model</p>
  
  <pre><code>class Product extends Eloquent {
       protected $table = 'products';
       protected $primaryKey = 'product_id';
  
       public function categories()
       {
           return $this-&gt;hasMany('Categories', 'category_id');
           //return $this-&gt;belongsToMany('Categories', 'category_id');
       }
  
       public $timestamps = false;
  }
  </code></pre>
  
  <p>And this is HomeController.php which load index.blade.php view</p>
  
  <pre><code>class HomeController extends BaseController {
  
     public function index()
     {                           
        $products = Product::where('category_id', '!=', 1)
            -&gt;with('category')
            -&gt;min('price')
            -&gt;get();          
  
              return View::make('site.index', [
                  'categories' =&gt; $categories
              ]); 
     }
  }
  </code></pre>
  
  <p>Right now when I load the page I get this error</p>
  
  <pre><code>production.ERROR: exception 'Symfony\Component\Debug\Exception\FatalErrorException' with message 'Call to a member function get() on a non-object' in ....
  </code></pre>
  
  <p>What is wrong here and how I can display categories and lowest price of each category?</p>
  
  <p>Update: This is how is look now</p>
  
  <p>Categories.php model</p>
  
  <pre><code>class Categories extends Eloquent {
      protected $table = 'category';
      protected $primaryKey = 'category_id';
  
      public $timestamps = false;
  
      public function products()
      {
          return $this-&gt;hasMany('Product', 'category_id');
      } 
  
      public function lowestProduct() {
          return $this-&gt;products()-&gt;selectRaw('*, max(price) as aggregate')
            -&gt;groupBy('products.product_id')-&gt;orderBy('aggregate');
      }
  }
  </code></pre>
  
  <p>HomeController.php</p>
  
  <pre><code>public function index()
  {
  
      $categories = Categories::with('lowestProduct')-&gt;get();         
       //$categories = Categories::paginate(15);
              return View::make('site.index', [
                  'categories' =&gt; $categories
      ]); 
  }
  </code></pre>