Posts Learn Components Snippets Categories Tags Tools About
/

How to Limit Laravel Eloquent Result

Learn how to limit laravel eloquent result using take, limit and pagination

Created on Nov 18, 2021

184 views

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

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

Load comments for How to Limit Laravel Eloquent Result

)