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

GregorSams's avatar

Use Pagination on Query Builder Instance

I am using Laravel Framework 8.62.0 and PHP 7.4.20.

I get the following error:

Call to undefined method Illuminate\Database\Eloquent\Builder::links() (View: /home//Code/test_project/resources/views/index.blade.php)

I have a view that has uses 3 simple filters. To display the view without filters I use the following:

    public function getSearchView()
    {
        try {

            $con = 'mysql_prod';

            // search results
            $items = Item::on($con)->select(['items.name AS item_name', 'items.slug', 'items.id AS item_id', 'item_details.sticker_number', 'item_details.section', 'item_details.type', 'collections.name AS collections_name', 'collections.sport_type', 'collections.league', 'collections.year as collections_year', 'images.file_name'])
                ->leftJoin('item_details', 'items.id', '=', 'item_details.items_id')
                ->leftJoin('collections', 'items.collections_id', '=', 'collections.id')
                ->leftJoin('images', 'images.items_id', '=', 'items.id')
                ->limit(500)
                ->paginate(10);

            // filter field
            $condition = Condition::on($con)->select(['id', 'name AS condition_name'])
                ->distinct()
                ->get();
            $collection = Collection::on($con)->select(['id', 'name AS collection_name'])
                ->distinct()
                ->orderBy('collection_name', 'ASC')
                ->get();

            return view('index', compact('items'));
        } catch (\Exception $e) {
            Log::error($e);
            report($e);
        }
    }

To filter the view I use:

   public function postFilter(Request $request)
    {
        try {

            $con = 'mysql_prod';

            //##################################
            // QUERY - SEARCH RESULTS
            //##################################

            $items = Item::on($con)->select(['items.name AS item_name', 'items.slug', 'items.id AS item_id', 'item_details.sticker_number', 'item_details.section', 'item_details.type', 'collections.name AS collections_name', 'collections.sport_type', 'collections.league', 'collections.year as collections_year', 'images.file_name'])
                ->leftJoin('item_details', 'items.id', '=', 'item_details.items_id')
                ->leftJoin('collections', 'items.collections_id', '=', 'collections.id')
                ->leftJoin('images', 'images.items_id', '=', 'items.id');

            // collection
            if(!is_null($request->select_collection_field)) $items->where('collections.id', '=', intval($request->select_collection_field));

            // FILTER field
            if(!is_null($request->select_filter_field)) {
                if($request->select_filter_field === "select_all") $items->orderBy('item_name', 'desc');
                if($request->select_filter_field === "publishing_year") $items->orderBy('collections_year', 'desc');
            }

            // query database
            $items->limit(500)->paginate(10);

            //##################################
            // FILTERS
            //##################################
            $condition = Condition::on($con)->select(['id', 'name AS condition_name'])
                ->distinct()
                ->get();
            $collection = Collection::on($con)->select(['id', 'name AS collection_name'])
                ->distinct()
                ->orderBy('collection_name', 'ASC')
                ->get();

            return view('index', compact('items', 'condition', 'collection'));

        } catch (\Exception $e) {
            Log::error($e);
            report($e);
        }
    }

In my web.php I have the two endpoints:

Route::get('/',  [SearchController::class, 'getSearchView'])->name('/');
Route::post('postFilter',  [SearchController::class, 'postFilter']);

In my view I use the pagination of laravel:

 {!! $items->links('vendor.pagination.default') !!}

Any suggestions why I get the above error and how to fix it?

I appreciate your replies!

0 likes
2 replies
Snapey's avatar

when you access WHICH route?

I don't believe you can use limit in the query since the paginator will overwrite it, but I doubt this is related to your issue

Why do you bother running $condition query and $collection query in your getSearch method when you don't use them?

Never return a view in response to a POST request. You should pass your search information as query string parameters

jlrdw's avatar

In the free laravel 8 from scratch video series Jeffrey as one video on pagination, I would suggest paginate like @jeffreyway teaches.

Of course with joins you may want a length aware paginator.

Please or to participate in this conversation.