MySQL laravel 4 - 如何限制(接受和跳过)Eloquent ORM?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18204342/
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
laravel 4 - how to Limit (Take and Skip) for Eloquent ORM?
提问by Johannes
TL;DR
TL; 博士
Can you limit an Eloquent ORM query like using take()
and skip()
so that the resulting mysql query is also limited, and it doesn't have to return the entire dataset?
你能限制像 using 一样的 Eloquent ORM 查询take()
,skip()
这样得到的 mysql 查询也受到限制,并且它不必返回整个数据集吗?
If so, how would you modify:
如果是这样,您将如何修改:
$test = User::find(1)->games->toArray();
To include limit 3 offset 2
?
包括limit 3 offset 2
?
Tables:
表格:
users games userGames
-- id -- id -- user_id
-- name -- name -- game_id
-- steam_id
Models:
楷模:
class User extends Eloquent {
public function games() {
return $this->belongsToMany('Game', 'userGames', 'user_id', 'game_id');
}
}
class Game extends Eloquent {
public function users() {
return $this->belongsToMany('User', 'userGames', 'user_id', 'game_id');
}
}
Limit in Query Builder
查询生成器中的限制
Using the regular Laravel Query BuilderI can get all games
that belong to user
of id 1, and limit the result with take()
and skip()
:
使用常规Laravel查询生成器,我可以得到所有games
属于user
ID 1,并限制其结果与take()
和skip()
:
$test = DB::table('games')
->join('userGames', 'userGames.game_id', '=', 'games.id')
->where('userGames.user_id', '=', '1')->take(3)->skip(2)->get();
By listening to the illuminate.query
event I can see that the query generated by this is:
通过监听illuminate.query
事件,我可以看到由此生成的查询是:
select * from `games`
inner join `userGames`
on `userGames`.`game_id` = `games`.`id`
where `userGames`.`user_id` = ?
limit 3 offset 2
Limit in Eloquent ORM
Eloquent ORM 中的限制
When I try to recreate the same query with Eloquent:
当我尝试使用 Eloquent 重新创建相同的查询时:
$test = User::find(1)->games->take(2)->toArray();
I'm able to use take
but adding skip
causes an error. Also the resulting query does not actually contain the limit:
我可以使用take
但添加skip
会导致错误。此外,结果查询实际上并不包含限制:
select `games`.*, `userGames`.`user_id` as `pivot_user_id`,
`userGames`.`game_id` as `pivot_game_id` from `games`
inner join `userGames`
on `games`.`id` = `userGames`.`game_id`
where `userGames`.`user_id` = ?
So it seems that the entire result is being queried first, which is not ideal when dealing with large data sets.
所以看起来是先查询整个结果,在处理大数据集的时候不太理想。
Question:
题:
Is it possible to limit an Eloquent ORM query so that at the MYSQL Query level it also limits the result, equivalent to limit 3 offset 2
?
是否可以限制 Eloquent ORM 查询,以便在 MYSQL 查询级别它也限制结果,相当于limit 3 offset 2
?
回答by daylerees
User::find(1)->games()->take(3)->skip(2)->get();
I think this should give you your collection. :)
我认为这应该给你你的收藏。:)
->games
will give you a collection, where ->games()
will offer a query builder instance.
->games
将为您提供一个集合,其中->games()
将提供一个查询构建器实例。
Enjoy Laravel!
享受 Laravel!