Tomas Shelby
Tomas Shelby

Reputation: 243

Method Illuminate\Database\Eloquent\Collection::skip does not exist - Laravel 5.8

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?

Upvotes: 1

Views: 6687

Answers (2)

Andy Song
Andy Song

Reputation: 4684

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

Upvotes: 4

fakorede
fakorede

Reputation: 783

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

Upvotes: 1

Related Questions