2017-09-05 20 views
1
過濾的foreach數據

我在表中的數據...看我下面的圖像與Laravel

enter image description here

我想在類別名稱過濾數據......,如果過濾器1數據,如「祝賀」 ......它的工作....看我的形象下面

enter image description here

,然後....我想過濾,如「恭喜你,叢林園,蘭花」,但它不能正常工作3個數據.....看看我的圖片

enter image description here

你能幫我解決這個問題.... 這是我的類別名稱過濾器的代碼在我的控制器

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

更多的細節......這是我所有的過濾代碼....看看我的代碼如下

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); 
 

 
    }

...這是我在查看代碼...看看下面

<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

我想'q-> where('name''指的是產品的__name__,而不是類別。 –

+0

@u_mulder但我的產品屬於類別...,如果只過濾1個數據,我就成功了....看看我的圖片 – FaizFurios52

回答

0

我的代碼,你可以用whereIn方法試試,它驗證一個列的值包含在給定的數組中

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