Home > other >  Laravel Pagination - Call to undefined method Illuminate\Database\Eloquent\Builder::links()
Laravel Pagination - Call to undefined method Illuminate\Database\Eloquent\Builder::links()

Time:10-25

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 via get 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!

CodePudding user response:

public function boot()
{
    Paginator::defaultView('view-name');

    Paginator::defaultSimpleView('view-name');
}

add this code to AppServiceProvider. I hope it will work.

CodePudding user response:

$items is currently a Query Builder instance. This object wont change, it will continue to be a Query Builder instance. When you execute a query from a Query Builder you get a returned result, and that is what you need to be passing to your view. You could reassign $items to this result easily:

$items = $items->limit(500)->paginate(10);

Now $items is the Paginator instance because you reassigned that variable to the result of the paginate call.

  • Related