2017-09-05 2 views
1

i ont des données dans le tableau ... regardez mon image ci-dessousdonnées foreach de filtrage avec Laravel

enter image description here

Je veux filtrer les données dans le nom de la catégorie ....., si le filtre 1 données comme « félicitation » ... son travail .... regarder mon image ci-dessous

enter image description here

puis .... Je veux filtrer 3 données comme « Félicitations, Bosquets, Orchidées », mais son travail ne ..... regardez mon image ci-dessous

enter image description here

Pouvez-vous me aider avec ce problème .... Ce est mon code de filtre de nom de la catégorie dans mon contrôleur

->whereHas('categories',function ($q) use ($filter){ 
 
       if(!empty ($filter['category_id'])) { 
 
        $q->where('name', 'Like', '%' . $filter['category_id'] . '%'); 
 
       } 
 
      })

pour plus de détails ... ceci est tout mon code de filtre .... regarde mon code ci-dessous

public function filter(request $request){ 
 

 
     $filter = $request->all(); 
 

 
     $products = Product::with('categories')->whereHas('store',function ($q) use($filter){ 
 

 
      if(!empty ($filter['store_id'])) { 
 
       $q->where('name', 'Like', '%' . $filter['store_id'] . '%'); 
 
      } 
 

 
     }) 
 
      ->whereHas('categories',function ($q) use ($filter){ 
 
       if(!empty ($filter['category_id'])) { 
 
        $q->where('name', 'Like', '%' . $filter['category_id'] . '%'); 
 
       } 
 
      }) 
 
      ->where(function ($q) use($filter){ 
 
       if(!empty ($filter['name'])){ 
 
        $q->where('name','Like','%'.$filter['name'].'%'); 
 
       } 
 
      }) 
 
      ->where(function ($q) use($filter){ 
 
       if(!empty ($filter['weight'])){ 
 
        $q->where('weight','Like','%'.$filter['weight'].'%'); 
 
       } 
 
      }) 
 
      ->where(function ($q) use($filter) { 
 
       if(!empty($filter['minimum_order'])) { 
 
        $q->where('minimum_order', '=', $filter['minimum_order']); 
 
       } 
 
      }) 
 

 
      ->where(function ($q) use($filter){ 
 
       if(!empty ($filter['id'])){ 
 
        $q->where('id','=', $filter['id']); 
 
       } 
 
      }) 
 

 
      ->where(function ($q) use($filter){ 
 
       if(!empty ($filter['price'])){ 
 
        $q->where('price','Like','%'.$filter['price'].'%'); 
 
       } 
 
      }) 
 
      ->where(function ($q) use($filter){ 
 
       if(!empty ($filter['total_sold'])){ 
 
        $q->where('total_sold','Like','%'.$filter['total_sold'].'%'); 
 
       } 
 
      }) 
 
      ->where(function ($q) use($filter){ 
 
       if(!empty ($filter['total_view'])){ 
 
        $q->where('total_view','Like','%'.$filter['total_view'].'%'); 
 
       } 
 
      }) 
 
      ->where(function ($q) use($filter){ 
 
       if(!empty ($filter['status'])){ 
 
        $q->where('status','Like','%'.$filter['status'].'%'); 
 
       } 
 
      })->with([ 
 
       'store','category' 
 
      ]) 
 
      ->paginate(10); 
 
      
 
      
 
     return view('products.index')->with('products', $products); 
 

 
    }

et ... ceci est mon code en mode ... regardez mon code ci-dessous

<table class="table table-responsive" id="products-table"> 
 
    <thead> 
 
     <th>Product Id</th> 
 
     <th>Store Name</th> 
 
     <th>Category Name</th> 
 
     <th>Product Name</th> 
 
     <!-- <th>Photo</th> 
 
     <th>Photo List</th> 
 
     <th>Description</th> --> 
 
     <th>Weight</th> 
 
     <!-- <th>Likes</th> --> 
 
     <th>Price</th> 
 
     <th>Total Sold</th> 
 
     <th>Total View</th> 
 
     <th>Status</th> 
 
     <th colspan="3">Action</th> 
 
    </thead> 
 
    <tbody> 
 
    
 
    @foreach($products as $product) 
 
     <tr> 
 
      <td> 
 
       @if(!empty ($product['id'])) 
 
        {{ $product->id }} 
 
       @endif 
 
      </td> 
 
      <td> 
 
       @if(!empty ($product['store_id'])) 
 
        {{ $product->store->name }} 
 
       @endif 
 
      </td> 
 
      <td> 
 
       @php 
 
        ob_start(); 
 
        foreach($product->categories as $category){ 
 
         echo $category->name.', '; 
 
        } 
 
        $output = ob_get_clean(); 
 
        echo rtrim($output,', '); 
 
       @endphp 
 

 
      </td> 
 
      <td> 
 
       {{ $product->name }} 
 
      </td> 
 
      <!-- <td>{!! $product->photo !!}</td> 
 
      <td>{!! $product->photo_list !!}</td> 
 
      <td>{!! $product->description !!}</td> --> 
 
      <td>     
 
       {{ $product->weight }} 
 
      </td> 
 
      <!-- <td>{!! $product->likes !!}</td> --> 
 
      <td> 
 
       {{ $product->price }} 
 
      </td> 
 
      <td> 
 
       {{ $product->total_sold }} 
 
      </td> 
 
      <td> 
 
       {{ $product->total_view }} 
 
      </td> 
 
      <td> 
 
       @if($product->status == 1) 
 
        {{ 'Active' }} 
 
       @elseif($product->status ==2) 
 
        {{ 'Inactive' }} 
 
       @endif 
 
      </td> 
 
      <td> 
 
       {!! Form::open(['route' => ['products.destroy', $product->id], 'method' => 'delete']) !!} 
 
       <div class='btn-group'> 
 
        <a href="{!! route('products.show', [$product->id]) !!}" class='btn btn-default btn-xs'><i class="glyphicon glyphicon-eye-open"></i></a> 
 
        <a href="{!! route('products.edit', [$product->id]) !!}" class='btn btn-default btn-xs'><i class="glyphicon glyphicon-edit"></i></a> 
 
        {!! Form::button('<i class="glyphicon glyphicon-trash"></i>', ['type' => 'submit', 'class' => 'btn btn-danger btn-xs', 'onclick' => "return confirm('Are you sure?')"]) !!} 
 
       </div> 
 
       {!! Form::close() !!} 
 
      </td> 
 
     </tr> 
 
    @endforeach 
 
    </tbody> 
 
</table>

+0

Je suppose '$ q-> where ('nom'' se réfère à __name__ du produit, pas de catégorie. –

+0

@u_mulder mais mon produit appartient à la catégorie ..., et je réussis si filtre juste 1 données .... regardez mon image ci-dessus – FaizFurios52

Répondre

0

Vous pouvez essayer avec la méthode whereIn, il vérifie que de une colonne donnée valeur est contenue dans le tableau donné

$users = DB::table('users') 
        ->whereIn('id', [1, 2, 3]) 
        ->get();