1
votes

I'm using laravel 5.8

I have this closed query:

$user = User::get();

and Then this doesn't work:

$user->skip(20)->take(10);

This occurs:

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

Tell me what should I do to achieve this without error?

2
What are you trying to achieve exactlythefabdev
@Abiola Trying to paginate a closed query.Tomas Shelby

2 Answers

3
votes

Tl;dr For 5.8, the solution is to use slice(); So it becomes to $user->slice(20)->take(10);

If you wonder why $users = User::skip(20)->take(10)->get(); works, but the code below is not working.

$user = User::get();
$user->skip(20)->take(10);

This is because, when you use Eloquent to query database many chainable methods (like: where, skip and many others) will translate to query builder, but when you call get it's going to return the whole result from database to your local memory, so it becomes to Illuminate\Database\Eloquent\Collectioninherents from Illuminate\Support\Collection.

For 5.8 the Collection does not have skip method. So you get that error.

It's added since 6, so you in order to achieve what you want you either update to 6 + or use slice().

1
votes

Not sure what you're trying to achieve but this should work

$users = User::skip(20)->take(10)->get();

This also works

$users = DB::table('users')->skip(20)->take(10)->get();