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

Waldemar's avatar

Filtered Data with pagination

Hi! Filter products work is fine, and pagination work is fine too. But when i first filter data and then try paginate filter droped Filter data and paginate i need see url as

?brands%5B%5D=2&page=2

but when i paginate filtered data i have url

?page=2

Filter is gone

HTML

@section('filters')
<div class="sidebar-module-container">
    <h3 class="section-title">Сортировать</h3>
    <div class="sidebar-filter">
<div class="sidebar-widget outer-bottom-xs wow fadeInUp">
    <div class="widget-header">
        <h4 class="widget-title">Производители</h4>
    </div>
    <div class="sidebar-widget-body m-t-20">
        <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>
    </div>
</div>
</div>
</div>
@stopassola 

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

CONTROLLER

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);
            
            if(!empty($products->currentPage()))
            {
                $pageNumber = $products->currentPage() == 1 ? '' : $products->currentPage(); 
            }

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

MODEL

class Products extends Model
{
    public $timestamps = false;
    protected $table = 'products';
    protected $fillable = ['title', 'path', 'preview', 'short_description', 'price', 'usd', 'parent_type', 'producer_id'];

    public function value()
    {
        return $this->hasOne('App\Values', 'product_id');
    }

    public function producer()
    {
        return $this->belongsTo('App\Categories', 'producer_id');
    }

    public function kind()
    {
        return $this->belongsTo('App\Categories', 'kind_id');
    }

    public function images()
    {
        return $this->hasMany('App\Images', 'product_id');
    }
}

Please help i need create filters as woocomerce with paginate! Maybe some one have a good tutorial or help with my code?

0 likes
9 replies
Waldemar's avatar

@SaeedPrez can you write the example for my code please! I can not figure out how to do it correctly for my code

SaeedPrez's avatar

@Deamonik

When you're using ->paginate() Laravel has a built in method for generating pagination links (default is with Bootstrap CSS).

For example, let's assume you query the products model in the controller with pagination and send the result to the view.

public function index() {
    $products = Product::where('foo', 'bar')->paginate(10);

    return view('products.index', compact('products'));
}

Then in the view you can use $products->links() to display the page links..

@foreach($products as $product)
    <lil>{{ $product->name }}</li>
@endforeach

{{ $products->links() }}

Now all these links generated by Laravel ->links() method will be /products/?page=X where X represents the page number.

If you would like to add for example brand=Y to the query string, you can use the ->appends() method..

{{ $products->appends(['brand' => 'Y'])->links() }}

The generated links will now be /products/?brand=Y&page=X where X represents the page number and Y the brand.

1 like
Waldemar's avatar

@SaeedPrez Ok its work! But i need add sort values relationship

model Products

 public function value()
    {
        return $this->hasOne('App\Values', 'product_id');
    }
SaeedPrez's avatar

@Deamonik you can add whatever you want... just send the data from the controller to the view and then include it in the appends()..

{{ $products->appends(['brand' => 'Y', 'sort' => $sort])->links() }}
Waldemar's avatar

@SaeedPrez

No its a different table values i need sort products where value_4 = input::get(filter value)

SaeedPrez's avatar

You can probably do something like..

$products = Product::whereHas(['value' => function($query) {
    $query->where('value_4', Input::get('filter_value'));
}])->paginate(10);
Waldemar's avatar
Waldemar
OP
Best Answer
Level 3

This is my decision

$products = Products::join('values', 'products.id', '=', 'values.product_id')->where('kind_id', $kind->id)->where(function($query){
                $brands = Input::has('brands') ? Input::get('brands') : null;
                $cokol = Input::has('cokol') ? Input::get('cokol') : null;
                $cvet = Input::has('cvet') ? Input::get('cvet') : null;

                if(isset($brands))
                {
                    foreach ($brands as $brand) {
                        $query->where('producer_id', '=', $brand);
                    }
                }
                if(isset($cokol))
                {
                    foreach ($cokol as $cok) {
                        $query->where('values.value_4', '=', $cok);
                    }
                }
                if(isset($cvet))
                {
                    foreach ($cvet as $cv) {
                        $query->where('values.value_9', '=', $cv);
                    }
                }
            })->orderBy('product_order', 'desc')->paginate(12);

I am add

Products::join('values', 'products.id', '=', 'values.product_id')

Please or to participate in this conversation.