php 雄辩的 Laravel:如何从 ->get() 获取行数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33676576/
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
eloquent laravel: How to get a row count from a ->get()
提问by JP Foster
I'm having a lot of trouble figuring out how to use this collection to count rows.
我在弄清楚如何使用这个集合来计算行数时遇到了很多麻烦。
$wordlist = \DB::table('wordlist')->where('id', '<=', $correctedComparisons)
->get();
I have tried adding->count()
but didn't work. I have tried doing count($wordlist)
. I'm not really sure what to do without needing a second request as a->count()
method.
我试过adding->count()
但没有奏效。我试过做count($wordlist)
。如果不需要第二个请求作为a->count()
方法,我真的不确定该怎么做。
回答by Thomas Kim
Answer has been updated
答案已更新
count
is a Collection method. The query builder returns an array. So in order to get the count, you would just count it like you normally would with an array:
count
是一个集合方法。查询构建器返回一个数组。因此,为了获得计数,您只需像通常使用数组一样进行计数:
$wordCount = count($wordlist);
If you have a wordlist model, then you can use Eloquent to get a Collection and then use the Collection's count
method. Example:
如果你有一个词表模型,那么你可以使用 Eloquent 来获取一个 Collection,然后使用 Collection 的count
方法。例子:
$wordlist = Wordlist::where('id', '<=', $correctedComparisons)->get();
$wordCount = $wordlist->count();
There is/was a discussion on having the query builder return a collection here: https://github.com/laravel/framework/issues/10478
有/曾经讨论过让查询构建器在此处返回集合:https: //github.com/laravel/framework/issues/10478
However as of now, the query builder always returns an array.
但是,截至目前,查询构建器始终返回一个数组。
Edit: As linked above, the query builder now returns a collection (not an array). As a result, what JP Foster was trying to do initially will work:
编辑:如上链接,查询构建器现在返回一个集合(不是数组)。因此,JP Foster 最初尝试做的事情会奏效:
$wordlist = \DB::table('wordlist')->where('id', '<=', $correctedComparisons)
->get();
$wordCount = $wordlist->count();
However, as indicated by Leon in the comments, if all you want is the count, then querying for it directly is much faster than fetching an entire collection and then getting the count. In other words, you can do this:
然而,正如 Leon 在评论中指出的那样,如果你想要的只是计数,那么直接查询它比获取整个集合然后获取计数要快得多。换句话说,你可以这样做:
// Query builder
$wordCount = \DB::table('wordlist')->where('id', '<=', $correctedComparisons)
->count();
// Eloquent
$wordCount = Wordlist::where('id', '<=', $correctedComparisons)->count();
回答by Parth kharecha
Direct get a count of row
直接获取行数
Using Eloquent
使用雄辩
//Useing Eloquent
$count = Model::count();
//example
$count1 = Wordlist::count();
Using query builder
使用查询构建器
//Using query builder
$count = \DB::table('table_name')->count();
//example
$count2 = \DB::table('wordlist')->where('id', '<=', $correctedComparisons)->count();
回答by Rejneesh Raghunath
Its better to access the count with the laravels count method
最好使用 laravel 计数方法访问计数
$count = Model::where('status','=','1')->count();
or
或者
$count = Model::count();
回答by Sayed Sajad Hosseini
also, you can fetch all data and count in the blade file. for example:
此外,您可以获取刀片文件中的所有数据和计数。例如:
your code in the controller
您在控制器中的代码
$posts = Post::all();
return view('post', compact('posts'));
your code in the blade file.
您在刀片文件中的代码。
{{ $posts->count() }}
finally, you can see the total of your posts.
最后,您可以看到您的帖子总数。
回答by Adnan Zaheer
it's work me
这对我有用
Step 1
第1步
Add in header
在标题中添加
<!-- CSRF Token -->
<meta name="csrf-token" content="{{ csrf_token() }}">
Step 2
第2步
use in div tag
在 div 标签中使用
<div class="mr-5">{{ \DB::table('customers')->count()}} Customers</div>