Carl Carl - 1 month ago 24
PHP Question

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.

Answer

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();