webfuelcode's avatar

List with paginate

I have a view with the category on the left and the list on left. But no pagination.

How to display links with paginating?

public function show(Link $link)
    {
        $link->load('category');
        return view('links.single', compact('link'));
    }
0 likes
11 replies
laracoft's avatar

What's Link?

Try

$link->load('category')->paginate(15);
MichalOravec's avatar

In the show method you have just one record of links. Better if you show all links with pagination in the index method like

public function index()
{
    $links = Link::paginate(10);
    
    return view('links.index', compact('links'));
}

In view

@foreach ($links as $link)
    //
@endforeach

{{ $links->links() }} // to show a pagination
webfuelcode's avatar

@michaloravec I posted the wrong one...here is the function which calls list when a category is clicked and in this, I want to show paginate.

public function show(Category $category)
    {
        $categories = Category::all();
        $links = $category->links();
        return view('links.category', compact('links', 'categories'));
    }
MichalOravec's avatar

If you want to paginate links of category then

public function show(Category $category)
{
    $categories = Category::all(); // this will be better if you use view composer for that
    
    $links = $category->links()->paginate(10);
    
    return view('links.category', compact('category', 'links', 'categories'));
}

Docs: https://laravel.com/docs/8.x/views#view-composers

laracoft's avatar

@webfuelcode

public function show(Category $category)
{
    $categories = Category::all();
    $links = $category->links()->paginate(10);
    return view('links.category', compact('links', 'categories'));
}

and in your blade

{{ $links->links() }} 
webfuelcode's avatar

It says

Method Illuminate\Database\Eloquent\Collection::paginate does not exist.

When adding $links = $category->links()->paginate(10);

MichalOravec's avatar

Do you have hasMany relationship links() in Category model?

Like

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Category extends Model
{
    public function links()
    {
        return $this->hasMany('App\Models\Link');
    }
}
webfuelcode's avatar

Thanks for the reply @michaloravec I got the solution for pagination. I got this... in category model

public function links()
    {
        return $this->hasMany(Link::class)->paginate(10);
    }

Another problem I see is the next page shows no category count. I have category names on the left and the post count on each category. But when the next page is clicked, all the post count shows zero.

MichalOravec's avatar

It should be only

public function links()
{
    return $this->hasMany(Link::class);
}

Then this will work in controller

$links = $category->links()->paginate(10);

How I said before for list of categories use view composers and there somthing like this

$categories = Category::withCount('posts')->get();
webfuelcode's avatar

@michaloravec I have used it in the AppServiceProvider like this...

view::composer(['links.category'], function($view){
            $view->with('categories', Category::withCount('links')->get());
        });

And the error appears

Method Illuminate\Database\Eloquent\Collection::getRelated does not exist.

MichalOravec's avatar

If you use view composer you don't have to have this in controller

$categories = Category::all();

Because if so, you replace $categories variable from view composer.

Please or to participate in this conversation.