horse
horse

Reputation: 725

I can't evaluate links in laravel 4 pagination

I have 2 tables: posts and comments. Post and Comment has one-to-many relationship.

I have managed to evaluate paginations of posts and comments with following code:

$posts = Post::with(array(
          'comments' => function($c) {
                            $c->paginate(5);
                        }))
          ->paginate(10);

First line below gives the links of $posts, but the second doesn't.

{{ $posts->links() }}
{{ $post->comments->links() }}

It gives the following error:

Call to undefined method Illuminate\Database\Eloquent\Collection::links()

How can I evaluate the links of comments of each post?

Note: {{ $post->comments }} gives the comments of the specified post.

Upvotes: 0

Views: 191

Answers (3)

fideloper
fideloper

Reputation: 12293

I think you have two things to figure out here:

  1. If you have posts paginated AND comments paginated, how do you go forward and back on posts and comments on the same web page? (Do you want to just limit the number of comments?
  2. I don't think Laravel will paginate on relationships like that (as evidenced by that not working).

I think we need to know what you want to have as a result of this query. In what scenario are you paginating both posts and its child comments at the same time?

Upvotes: 0

Learning Laravel
Learning Laravel

Reputation: 11

Try to use

{{ $posts->comments->link }}

if it's not working, try:

{{ $posts->comments()->link }}

Make sure you've set up the relationship correctly first.

Upvotes: 1

Danzabar
Danzabar

Reputation: 31

Try

{{ $post->comments()->links() }}

Upvotes: 0

Related Questions