duancanjiu3754 2017-09-05 07:18
浏览 57

使用Laravel过滤foreach数据

i have data in table...look my image below

enter image description here

I want filter data in category name.....,if filter 1 data like "congratulation"...its work....look my image below

enter image description here

and then....I want filter 3 data like "Congratulations, Bosquets, Orchids" but its not work.....look my image below

enter image description here

Can you help me with this problem.... This is my category name filter code in my controller

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

for more detail...this is all my filter code....look my code below

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

    }

and...this is my code in view...look my code below

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

</div>
  • 写回答

1条回答 默认 最新

  • doulin4844 2017-09-05 07:42
    关注

    You can try with whereIn method, it verifies that a given column's value is contained within the given array

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

报告相同问题?