MySQL 有没有办法用 Laravel 的 ELOQUENT ORM 来“限制”结果?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15229303/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 16:46:39  来源:igfitidea点击:

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

mysqllaraveleloquentsql-limit

提问by Natan Shalva

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

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

 SELECT * FROM  `games` LIMIT 30 , 30 

And with Eloquent ?

和雄辩?

回答by Muhammad Usman

Create a Game model which extends Eloquent and use this:

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

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

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

take()此处将获得 30 条记录,skip()此处将偏移为 30 条记录。



In recent Laravel versions you can also use:

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

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

回答by fregante

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

回答by srmilon

We can use LIMIT like bellow:

我们可以像下面这样使用 LIMIT:

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