title: Laravel Search Relations whereHas
body: <p>I am trying to do a search on products but it does not return any results. I suspect it has something to do with the whereHas function but I just cannot figure it out. Here is my code:</p>
  
  <p>My Relations:</p>
  
  <p>Categories Model:</p>
  
  <pre><code>class Categories extends Eloquent {
  
      public function product()
      {
          return $this-&gt;hasMany('Products')-&gt;orderBy('name');
      }
  }
  </code></pre>
  
  <p>Products Model:</p>
  
  <pre><code>class Products extends Eloquent {
  
      public function category()
      {
          return $this-&gt;belongsTo('Category')-&gt;orderBy('name');
      }
  }
  </code></pre>
  
  <p>My search function:</p>
  
  <pre><code>public function search()
  {
      $search         = Input::get('search');
  
      $result             = Products::where('name', 'LIKE', '%'. $search .'%')-&gt;get();
  
      if ($result-&gt;first()) {
  
          return View::make('groceries.index')
          -&gt;with('categories', Categories::with('product')-&gt;orderBy('name', 'asc')
          -&gt;whereHas('product', function($query) use ($search)
          {
              $query-&gt;where('name', 'LIKE', '%'.$search.'%');
          }));
      }
  }
  </code></pre>
  
  <p>In the view:</p>
  
  <pre><code>@foreach ($categories as $category)
  
  //do stuff
  
      @foreach ($category-&gt;product as $products)
  
  //Show results
  
      @endforeach
  
  @endforeach
  </code></pre>