Home / Snippets / How to Limit Laravel Eloquent Result
How to Limit Laravel Eloquent Result cover

How to Limit Laravel Eloquent Result

197

2 years ago

0 comments

Sometimes you might want to limit the eloquent result returned from the DB and to do that you can specify using the "take()" method or the "limit()" method.

Limit Eloquent Result Using Using take() and limit()


For example, let's say you want to limit the querying number of "posts" and to do that you can write your code like the following.
<?php

use App\Models\Post;

// using take()
Post::query()->take(20)->get();

// using limit()
Post::query()->limit(20)->get();

Limit Eloquent Result Using Pagination


Sometimes you might also want to use the pagination approach and to do that you can write your code like below.
<?php

use App\Models\Post;

$posts = Post::query()->paginate(20);
notion avatar

Alaz

Week-end developer currently experimenting with web, mobile, and all things programming.

Topics:

Frontend

Resource

Average

Average

Support Us

If you like our tutorial, support us by being our Patreon or buy us some coffee ☕️

Welcome to PostSrc V3

PostSrc Dark Logo

You have to login to favorite this