使用Laravel过滤foreach数据

时间:2017-09-05 07:18:35

标签: php laravel-5.3 blade

我的数据在表格中......看下面的图片

enter image description here

我想要类别名称中的过滤数据.....,如果过滤1数据,例如"祝贺" ...它的工作......看下面我的图像

enter image description here

然后......我希望过滤器3数据像#34;祝贺,Bosquets,Orchids"但它不起作用.....看下面的图像

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>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

您可以尝试使用whereIn方法,它会验证给定列的值是否包含在给定数组中

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