Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Waldemar's avatar

Filters paginate

Hi! How i can paginate filtered products?

$products = Products::where('kind_id', $kind->id)->where(function($query){
                $brands = Input::get('brands');
                if(isset($brands)){
                    foreach ($brands as $brand) {
                        $query->orWhere('producer_id', '=', $brand)->orderBy('product_order', 'desc')->paginate(12);
                    }
                }
            })->orderBy('product_order', 'desc')->paginate(12);

Products filter work is fine but paginate droped the filters paginate have a url

?page=2

Filters is gone

0 likes
8 replies
sandersjj's avatar

I guess you would pass the filter parameters in the get request.

Waldemar's avatar

@dubbeltje My HTML i try with GET method POST method and delete method not working =(

<form action="" method="get" accept-charset="utf-8">
            <ul>
                
                @foreach($producers->producerToKind as $producer)
                <li><input type="checkbox" name="brands[]" value="{{ $producer->id }}"> {{ $producer->title }}</li>
                @endforeach
            </ul>
        <button type="submit">Sort</button>
getvma's avatar

@Deamonik Store $brands in a session. Then use the data stored in the session for your query.

getvma's avatar

I am assuming you are in a controller method. Actually I am assuming alot of other stuff but this is the idea.

$request->session()->put('brands', '');

        if(isset(Input::get('brands'))){
            foreach($brands as $brand) {
                $request->session()->push('brands', $brand);
            }

        }
        $products = Products::where('kind_id', $kind->id)->where(function($query){
            $brands = $request->session()->get('brands');
            if(isset($brands)){
                foreach ($brands as $brand) {
                    $query->orWhere('producer_id', '=', $brand)->orderBy('product_order', 'desc')->paginate(12);
                }
            }
        })->orderBy('product_order', 'desc')->paginate(12);

However, you really should be looking into parameters. That's the way to go for the filtered results. With the filtered parameters on the URL you or your guests can bookmark your results. Using this method you are limited to the session.

Waldemar's avatar

still doesn`t work =( This is full code

public function index($kind, Request $request)
    {
        $kind = Categories::where('path', $kind)->first();

        if($kind == NULL or $kind->type == 'producer' or $kind == FALSE)
        {
            return abort(404);
        } else {
            $producers = Categories::find($kind->id);
            // $products = Products::where('kind_id', $kind->id)->orderBy('product_order', 'desc')->paginate(12);

            $brands = Input::has('brands') ? Input::get('brands') : null;

            $products = Products::where('kind_id', $kind->id)->where(function($query){
                $brands = Input::has('brands') ? Input::get('brands') : null;
                if(isset($brands)){
                    foreach ($brands as $brand) {
                        $query->orWhere('producer_id', '=', $brand);
                    }
                }
            })->orderBy('product_order', 'desc')->paginate(12);

            return view('categories.index', compact(['products']))
                ->with('category', $kind)
                ->with('producers', $producers)->withInput(Input::all());
        }
    }

HTML

@include('pagination', ['paginator' => $products])
@foreach($products as $data)
{{ $data->title }}
{{ $data->description }}
    ...
@endforeach 

What i do is wrong?

FORGOT HTML SORT

<form action="" method="GET" accept-charset="utf-8">
            <ul>
                <?php $brands = Input::has('brands') ? Input::get('brands'): [] ; ?>
                @foreach($producers->producerToKind as $producer)
                <li><input type="checkbox" name="brands[]" value="{{ $producer->id }}" {{ in_array($producer->id, $brands) ? 'checked' : '' }}> {{ $producer->title }}</li>
                @endforeach
            </ul>
        <button type="submit">Sort</button>

Please or to participate in this conversation.