title: filtering foreach data with Laravel
body: <p>i have data in table...look my image below</p>
  
  <p><a href="https://i.stack.imgur.com/PXksN.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/PXksN.png" alt="enter image description here"></a></p>
  
  <p>I want filter data in category name.....,if filter 1 data like "congratulation"...its work....look my image below</p>
  
  <p><a href="https://i.stack.imgur.com/9WpSK.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/9WpSK.png" alt="enter image description here"></a></p>
  
  <p>and then....I want filter 3 data like "Congratulations, Bosquets, Orchids" but its not work.....look my image below</p>
  
  <p><a href="https://i.stack.imgur.com/bj9Ai.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/bj9Ai.png" alt="enter image description here"></a></p>
  
  <p>Can you help me with this problem....
  This is my category name filter code in my controller</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>-&gt;whereHas('categories',function ($q) use ($filter){
                  if(!empty ($filter['category_id'])) {
                      $q-&gt;where('name', 'Like', '%' . $filter['category_id'] . '%');
                  }
              })</code></pre>
  </div>
  </div>
  </p>
  
  <p>for more detail...this is all my filter code....look my code below</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code> public function filter(request $request){
  
          $filter =  $request-&gt;all();
  
          $products  =  Product::with('categories')-&gt;whereHas('store',function ($q) use($filter){
  
              if(!empty ($filter['store_id'])) {
                  $q-&gt;where('name', 'Like', '%' . $filter['store_id'] . '%');
              }
  
          })
              -&gt;whereHas('categories',function ($q) use ($filter){
                  if(!empty ($filter['category_id'])) {
                      $q-&gt;where('name', 'Like', '%' . $filter['category_id'] . '%');
                  }
              })
              -&gt;where(function ($q) use($filter){
                  if(!empty ($filter['name'])){
                      $q-&gt;where('name','Like','%'.$filter['name'].'%');
                  }
              })
              -&gt;where(function ($q) use($filter){
                  if(!empty ($filter['weight'])){
                      $q-&gt;where('weight','Like','%'.$filter['weight'].'%');
                  }
              })
              -&gt;where(function ($q) use($filter) {
                  if(!empty($filter['minimum_order'])) {
                      $q-&gt;where('minimum_order', '=', $filter['minimum_order']);
                  }
              })
  
              -&gt;where(function ($q) use($filter){
                  if(!empty ($filter['id'])){
                      $q-&gt;where('id','=', $filter['id']);
                  }
              })
  
              -&gt;where(function ($q) use($filter){
                  if(!empty ($filter['price'])){
                      $q-&gt;where('price','Like','%'.$filter['price'].'%');
                  }
              })
              -&gt;where(function ($q) use($filter){
                  if(!empty ($filter['total_sold'])){
                      $q-&gt;where('total_sold','Like','%'.$filter['total_sold'].'%');
                  }
              })
              -&gt;where(function ($q) use($filter){
                  if(!empty ($filter['total_view'])){
                      $q-&gt;where('total_view','Like','%'.$filter['total_view'].'%');
                  }
              })
              -&gt;where(function ($q) use($filter){
                  if(!empty ($filter['status'])){
                      $q-&gt;where('status','Like','%'.$filter['status'].'%');
                  }
              })-&gt;with([
                  'store','category'
              ])
              -&gt;paginate(10);
              
              
           return view('products.index')-&gt;with('products', $products);
  
      }</code></pre>
  </div>
  </div>
  </p>
  
  <p>and...this is my code in view...look my code below</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;table class="table table-responsive" id="products-table"&gt;
      &lt;thead&gt;
          &lt;th&gt;Product Id&lt;/th&gt;
          &lt;th&gt;Store Name&lt;/th&gt;
          &lt;th&gt;Category Name&lt;/th&gt;
          &lt;th&gt;Product Name&lt;/th&gt;
          &lt;!-- &lt;th&gt;Photo&lt;/th&gt;
          &lt;th&gt;Photo List&lt;/th&gt;
          &lt;th&gt;Description&lt;/th&gt; --&gt;
          &lt;th&gt;Weight&lt;/th&gt;
          &lt;!-- &lt;th&gt;Likes&lt;/th&gt; --&gt;
          &lt;th&gt;Price&lt;/th&gt;
          &lt;th&gt;Total Sold&lt;/th&gt;
          &lt;th&gt;Total View&lt;/th&gt;
          &lt;th&gt;Status&lt;/th&gt;
          &lt;th colspan="3"&gt;Action&lt;/th&gt;
      &lt;/thead&gt;
      &lt;tbody&gt;
      
      @foreach($products as $product)
          &lt;tr&gt;
              &lt;td&gt;
                  @if(!empty ($product['id']))
                      {{ $product-&gt;id }}
                  @endif
              &lt;/td&gt;
              &lt;td&gt;
                  @if(!empty ($product['store_id']))
                      {{ $product-&gt;store-&gt;name }}
                  @endif
              &lt;/td&gt;
              &lt;td&gt;
                  @php
                     ob_start();
                     foreach($product-&gt;categories as $category){
                          echo $category-&gt;name.', ';
                     }
                     $output = ob_get_clean();
                     echo rtrim($output,', ');
                  @endphp   
  
              &lt;/td&gt;
              &lt;td&gt;
                  {{ $product-&gt;name }}
              &lt;/td&gt;
              &lt;!-- &lt;td&gt;{!! $product-&gt;photo !!}&lt;/td&gt;
              &lt;td&gt;{!! $product-&gt;photo_list !!}&lt;/td&gt;
              &lt;td&gt;{!! $product-&gt;description !!}&lt;/td&gt; --&gt;
              &lt;td&gt;                
                  {{ $product-&gt;weight }}
              &lt;/td&gt;
              &lt;!-- &lt;td&gt;{!! $product-&gt;likes !!}&lt;/td&gt; --&gt;
              &lt;td&gt;
                  {{ $product-&gt;price }}
              &lt;/td&gt;
              &lt;td&gt;
                  {{ $product-&gt;total_sold }}
              &lt;/td&gt;
              &lt;td&gt;
                  {{ $product-&gt;total_view }}
              &lt;/td&gt;
              &lt;td&gt;
                  @if($product-&gt;status == 1)
                      {{ 'Active' }}
                  @elseif($product-&gt;status ==2)
                      {{ 'Inactive' }}
                  @endif
              &lt;/td&gt;
              &lt;td&gt;
                  {!! Form::open(['route' =&gt; ['products.destroy', $product-&gt;id], 'method' =&gt; 'delete']) !!}
                  &lt;div class='btn-group'&gt;
                      &lt;a href="{!! route('products.show', [$product-&gt;id]) !!}" class='btn btn-default btn-xs'&gt;&lt;i class="glyphicon glyphicon-eye-open"&gt;&lt;/i&gt;&lt;/a&gt;
                      &lt;a href="{!! route('products.edit', [$product-&gt;id]) !!}" class='btn btn-default btn-xs'&gt;&lt;i class="glyphicon glyphicon-edit"&gt;&lt;/i&gt;&lt;/a&gt;
                      {!! Form::button('&lt;i class="glyphicon glyphicon-trash"&gt;&lt;/i&gt;', ['type' =&gt; 'submit', 'class' =&gt; 'btn btn-danger btn-xs', 'onclick' =&gt; "return confirm('Are you sure?')"]) !!}
                  &lt;/div&gt;
                  {!! Form::close() !!}
              &lt;/td&gt;
          &lt;/tr&gt;
      @endforeach
      &lt;/tbody&gt;
  &lt;/table&gt;</code></pre>
  </div>
  </div>
  </p>