php 在使用“take”和“skip”之前获取 Laravel 的 Eloquent 中的行数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25771314/
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
Get number of rows in Laravel's Eloquent before using "take" and "skip"
提问by Gga
I want to query my Laravel model using eloquent for results that may need to match some where clauses, then take
and skip
predefined numbers.
我想使用 eloquent 查询我的 Laravel 模型,以获取可能需要匹配某些 where 子句、thentake
和skip
预定义数字的结果。
This isn't a problem in itself, but I also need to know the number of rows that were found in the query before reducing the result set with take and skip - so the original number of matches which could be every row in the table if no where clauses are used or a few if either is used.
这本身不是问题,但我还需要知道在使用 take 和 skip 减少结果集之前在查询中找到的行数 - 所以原始匹配数可能是表中的每一行,如果没有使用 where 子句,或者如果使用了几个。
What I want to do could be accomplished by making the query twice, with the first omitting "->take($iDisplayLength)->skip($iDisplayStart)
" at the end and counting that, but that just seems messy.
我想要做的可以通过两次查询来完成,第一次->take($iDisplayLength)->skip($iDisplayStart)
在末尾省略“ ”并计算它,但这看起来很混乱。
Any thoughts?
有什么想法吗?
$contacts = Contact::where(function($query) use ($request)
{
if (!empty($request['firstname'])) {
$query->where(function($query) use ($request)
{
$query->where('firstname', 'LIKE', "%{$request['firstname']}%");
});
}
if (!empty($request['lastname'])) {
$query->where(function($query) use ($request)
{
$query->where('lastname', 'LIKE', "%{$request['lastname']}%");
});
}
})
->take($iDisplayLength)->skip($iDisplayStart)->get();
$iTotalRecords = count($contacts);
回答by Jarek Tkaczyk
You can use count
then get
on the same query.
您可以在同一查询中使用count
then get
。
And by the way, your whole query is a bit over complicated. It results in something like this:
顺便说一下,您的整个查询有点过于复杂。结果是这样的:
select * from `contacts` where ((`firstname` like ?) and (`lastname` like ?)) limit X, Y
Closure in where
is used to make a query like this for example:
Closure inwhere
用于进行这样的查询,例如:
select * from table where (X or Y) and (A or B);
So to sum up you need this:
所以总而言之,你需要这个:
$query = Contact::query();
if (!empty($request['firstname'])) {
$query->where('firstname', 'like', "%{$request['firstname']}%");
}
if (!empty($request['lastname'])) {
$query->where('lastname', 'like', "%{$request['lastname']}%");
}
$count = $query->count();
$contacts = $query->take($iDisplayLength)->skip(iDisplayStart)->get();
回答by user1669496
The Collection
class offers a splice and a count method which you can take advantage of.
该Collection
课程提供了一个拼接和计数方法,您可以利用它们。
First you would want to get the collection..
首先你会想要得到这个集合..
$collection = $query->get();
Then you can get the count and splice it.
然后你可以得到计数并拼接它。
$count = $collection->count();
$records = $collection->splice($iDisplayStart, $iDisplayLength);
This might be hard on performance because you are querying for the entire collection each time rather than putting a limit on the query, so it might be beneficial to cache the collection if this page is going to be hit often. At the same time though, this will hit the database only once, so it's a bit of a trade off.
这可能对性能造成影响,因为您每次都在查询整个集合,而不是对查询设置限制,因此如果此页面经常被点击,缓存集合可能会有所帮助。但与此同时,这只会访问数据库一次,所以这有点权衡。