Carlos R.
Carlos R.

Reputation: 23

Laravel eloquent skip method


I've came into a issue while using laravel eloquent ORM.
Basically, I'm obtaining the posts related to a user, as well his comments, however, I'm trying to integrate some kind of pagination(I only want to take 5 posts at a time).
While using the skip method, I'm always getting the following laravel error:

Method skip does not exist.

Here's the code snippet I'm using actually.

$posts = $account->posts->skip($page * 5)->take(5);

Could anyone give me any kind of assistance?
The account model retrieve the posts models related to the user(Relationship), however, I would like to only received 5 posts at a time, to use it on ajax based requests.

Upvotes: 2

Views: 4641

Answers (3)

weekapaug
weekapaug

Reputation: 332

Old post but I found another answer to be if you are using a laravel collection, you can use slice() instead of skip() - Using code at the top you could also

$posts = $account->posts->slice($page * 5)->take(5);

More Info

Laracasts Slice

Upvotes: 0

Alexey Mezenin
Alexey Mezenin

Reputation: 163858

Looks like you're trying to use skip() on a collection.

Try this instead:

$account->posts()->skip($page * 5)->take(5);

Or:

Account::with(['posts' => function($q) {
    $q->skip($page * 5)->take(5);
}])->get();

Upvotes: 3

aleksejjj
aleksejjj

Reputation: 1835

I'm trying to integrate some kind of pagination

Why you don't using paginate in this case? Something like

$account->posts()->paginate(5);

Upvotes: 0

Related Questions