Home > Blockchain >  Laravel: Pagination error when clicking category page
Laravel: Pagination error when clicking category page

Time:10-14

I'm successfully making pagination, but when I click a category I got an error message:

BadMethodCallException Method
Illuminate\Database\Eloquent\Collection::currentPage does not exist.

This is my view:

Halaman: {{$gmproducts->currentPage()}}<br/>
Jumlah data: {{$gmproducts->total()}}<br/>
Data perhalaman: {{$gmproducts->perPage()}}<br/>
{{$gmproducts->links()}}

This is my controller:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Gmproducts;
use App\Gmcategories;

class GoldmartController extends Controller
{
    public function index()
    {
        $gmproducts   = Gmproducts::orderBy('id', 'desc')->paginate(10);
        $gmcategories   = Gmcategories::all();

        return view('frontend.pages.goldmart', compact('gmproducts', 'gmcategories'));
    }

    public function readCategory($slug)
    {
        $category = Gmcategories::where('slug', $slug)->first();
        $gmcategories = Gmcategories::all();
        if($gmcategories)
        {
            $gmproducts = Gmproducts::where('category_id', $category->id)->get();
            return view('frontend.pages.goldmart', compact('gmproducts', 'category', 'gmcategories'));
        }
        else
        {
            return redirect('/');
        }
    }
}

CodePudding user response:

In your readCategory function $gmproducts is not paginated:

public function readCategory($slug)
    {
        $category = Gmcategories::where('slug', $slug)->first();
        $gmcategories = Gmcategories::all();
        if($gmcategories)
        {
            $gmproducts = Gmproducts::where('category_id', $category->id)->paginate(10);
            return view('frontend.pages.goldmart', compact('gmproducts', 'category', 'gmcategories'));
        }
        else
        {
            return redirect('/');
        }
    }
  • Related