0
votes

I am trying to paginate my categories shop.blade.php but I get this error:

Call to undefined method Illuminate\Database\Eloquent\Builder::links() (View: D:\xampp\htdocs\mieaceh\resources\views\shop.blade.php)

Here is my MenuController.blade.php

if (request()->category) {
  $menu = Menu::with('categories')->whereHas('categories', function ($query) {
    $query->where('slug', request()->category);
  });

  $categories = Category::all();
  $categoryName = $categories->where('slug', request()->category)->first()->name;
} else {
  $menu = Menu::inRandomOrder()->take(10)->paginate(8);
  $categories = Category::all();
  $categoryName = 'Featured';
}

return view('shop')->with([
        'menu' => $menu,
        'categories' => $categories,
        'categoryName' => $categoryName,
]);

and my shop.blade.php

<div class="category text-center">
@foreach($categories as $category)
<a href="{{ route('menu.index', ['category' => $category->slug]) }}">
    <div class="card-category" style="width: 10rem; height: 4rem;">
        {{ $category->name }}
    </div>
</a>
@endforeach
{{ $menu->links() }}

How to solve it?

2

2 Answers

0
votes

Right now, if you have a request()->category, then you're not adding the paginate to the $menu query. Just add it

 $menu = Menu::with('categories')->whereHas('categories', function ($query) {
        $query->where('slug', request()->category);
    })->paginate(8);
0
votes

You are missing pagiante() method on your menu query:

$menu = Menu::with('categories')->whereHas('categories', function ($query) {
            $query->where('slug', request()->category);
        })->paginate(8);

The way you did your query, you were returning QueryBuilder class, which has no links() method (pagination). This way you will return collection of data which you will be able to paginate.