DEV Community

Morcos Gad
Morcos Gad

Posted on

Laravel Eloquent Skip and Take - Offset and Limit

Let's start quickly. If you want to leave some columns in the database and take a few, is there a problem ? of course not
Skip and Take

User::skip(4)->take(6)->get();
DB::table('users')->skip(4)->take(6)->get();
Enter fullscreen mode Exit fullscreen mode

Offset and Limit

User::offset(4)->limit(6)->get();
DB::table('users')->offset(4)->limit(6)->get();
Enter fullscreen mode Exit fullscreen mode

take can be used within a relationship to extract a certain number of products

@foreach ($categories as $category)
  @foreach ($category->products->take(4) as $product)
     // fields
  @endforeach 
@endforeach 
Enter fullscreen mode Exit fullscreen mode

I hope you enjoyed the code.

Top comments (1)

Collapse
 
hammykl20 profile image
Hammy.kl20

good info