Natan Shalva
Natan Shalva

Reputation: 1572

Is there a way to "limit" the result with ELOQUENT ORM of Laravel?

Is there a way to "limit" the result with ELOQUENT ORM of Laravel?

 SELECT * FROM  `games` LIMIT 30 , 30 

And with Eloquent ?

Upvotes: 145

Views: 202361

Answers (4)

Krishnamoorthy Acharya
Krishnamoorthy Acharya

Reputation: 4244

Also, we can use it following ways

To get only first

 $cat_details = DB::table('an_category')->where('slug', 'people')->first();

To get by limit and offset

$top_articles = DB::table('an_pages')->where('status',1)->limit(30)->offset(0)->orderBy('id', 'DESC')->get();
$remaining_articles = DB::table('an_pages')->where('status',1)->limit(30)->offset(30)->orderBy('id', 'DESC')->get();

Upvotes: 0

Md. Saidur Rahman Milon
Md. Saidur Rahman Milon

Reputation: 2911

We can use LIMIT like bellow:

Model::take(20)->get();

Upvotes: 20

fregante
fregante

Reputation: 31698

If you're looking to paginate results, use the integrated paginator, it works great!

$games = Game::paginate(30);
// $games->results = the 30 you asked for
// $games->links() = the links to next, previous, etc pages

Upvotes: 19

Muhammad Usman
Muhammad Usman

Reputation: 12503

Create a Game model which extends Eloquent and use this:

Game::take(30)->skip(30)->get();

take() here will get 30 records and skip() here will offset to 30 records.


In recent Laravel versions you can also use:

Game::limit(30)->offset(30)->get();

Upvotes: 255

Related Questions