Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I skip n items from laravel eloquent collection?

Why this is not working?

$myFriends =Friend::where('status',1)->pluck('user_id');
$users = User::Where('active',1)->WhereNotIn('id',$myFriends)->get();     
$users =$users->skip(2)->take(3);

it gives following error

BadMethodCallException in Macroable.php line 74:
Method Skip does not exist.
like image 692
Sanjay Raut Avatar asked Nov 28 '22 00:11

Sanjay Raut


1 Answers

Use slice method instead of skip.

$users->slice(2)->take(3);

Or in a shorthand:

$users->slice(2, 3);

It skips 2 first records and returns next 3 records.


With respect to @ikurcubic's comment The answer updated.

like image 74
Khalil Laleh Avatar answered Dec 04 '22 04:12

Khalil Laleh