有没有办法“限制"?Laravel ELOQUENT ORM 的结果?

2021-12-26 00:00:00 mysql laravel eloquent sql-limit

有没有办法用 Laravel 的 ELOQUENT ORM 来限制"结果?

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

 SELECT * FROM  `games` LIMIT 30 , 30 

还有 Eloquent 吗?

And with Eloquent ?

推荐答案

创建一个扩展 Eloquent 的游戏模型并使用它:

Create a Game model which extends Eloquent and use this:

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

take() 这里会得到 30 条记录,skip() 这里会偏移到 30 条记录.

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

在最近的 Laravel 版本中,您还可以使用:

In recent Laravel versions you can also use:

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

相关文章