title: Laravel 5: Check if a category collection has a valid result or no records at all
body: <p>A very casual problem with the multi level DB tables and eloquent system.</p>
  <blockquote>
  <p>A MainCategory has more than one Category(<code>categories</code> table)</p>
  <p>A Category has more than one Product(<code>products</code> table)</p>
  </blockquote>
  <p>Now the <strong>routes.php</strong> has:</p>
  <pre><code>Route::get('/categories/{slug}', ['as' =&gt; 'categories.list', 'uses' =&gt; 'ListController@getCategories'])-&gt;where('slug', '[\w\d\-\_]+');
  </code></pre>
  <p>The <strong><code>ListController@getCategories</code></strong> method wants to list all <strong><code>products</code></strong> along with the <strong><code>categories</code></strong> come under the <strong><code>main_categories</code></strong>.</p>
  <pre><code>class ListController extends Controller
  {
      public function getCategories($slug) {
          $categories = Category::orderBy('id', 'asc')
              -&gt;select('categories.id as cat_id', 'categories.name as cat_name','categories.slug as cat_slug','products.*')
              -&gt;join('main_categories', 'main_categories.id', '=', 'categories.main_category_id')
              -&gt;join('products', 'products.category_id', '=', 'categories.id')
              -&gt;where('main_categories.slug', '=', $slug)
              -&gt;get();
          if(count($categories) &gt;  0) {
              return view('pages.categories')-&gt;withCategories($categories);
          }
          else { // either main_categories.name not in $slug or there is no category available for this main_category
  
          }
      }
  }
  </code></pre>
  <p>The code works fine as expected, listing both products and categories from the same collection object.</p>
  <p>Now there is an else case. How could I check the result collection so that I can differentiate if there was a <em>main_category</em> in the name of given <code>$slug</code> or if there was no <em>categories</em> under the <em>main_category</em> with <code>$slug</code>.</p>
  <p>See the code, <code>count($categories)</code> returns <code>0</code> for both cases. I don't stick on my eloquent way. You may suggest me another query or a condition check with PHP.
  I will appreciate anything that helps me to prevent this into further complexity.</p>
  <p>[PS:<em>All relations are direct and pretty basic(<code>belongsTo</code> and <code>hasMany</code>) and table names are in common standard- the Laravel 5 way of doing it(using v5.2). I guess you will get if there is a line of code needed/missing in my question. Feel free to ask if you still need anything.</em>]</p>