Php 雄辩的laravel:如何从a->;中获取行计数;得到()
我很难弄清楚如何使用这个集合来计算行数Php 雄辩的laravel:如何从a->;中获取行计数;得到(),php,sql,laravel,eloquent,Php,Sql,Laravel,Eloquent,我很难弄清楚如何使用这个集合来计算行数 $wordlist = \DB::table('wordlist')->where('id', '<=', $correctedComparisons) ->get(); $wordlist=\DB::table('wordlist')->其中('id','答案已更新 count是一种收集方法。查询生成器返回一个数组。因此,为了获得计数,您只需像通常使用数组一样对其进行计数: $wordCount = c
$wordlist = \DB::table('wordlist')->where('id', '<=', $correctedComparisons)
->get();
$wordlist=\DB::table('wordlist')->其中('id','答案已更新
count
是一种收集方法。查询生成器返回一个数组。因此,为了获得计数,您只需像通常使用数组一样对其进行计数:
$wordCount = count($wordlist);
如果您有一个单词列表模型,那么您可以使用Eloquent获取集合,然后使用集合的count
方法。例如:
$wordlist = Wordlist::where('id', '<=', $correctedComparisons)->get();
$wordCount = $wordlist->count();
$wordlist=wordlist::where('id','直接获取行的计数
使用雄辩的
//Useing Eloquent
$count = Model::count();
//example
$count1 = Wordlist::count();
使用查询生成器
//Using query builder
$count = \DB::table('table_name')->count();
//example
$count2 = \DB::table('wordlist')->where('id', '<=', $correctedComparisons)->count();
//使用查询生成器
$count=\DB::table('table_name')->count();
//范例
$count2=\DB::table('wordlist')->where('id','最好使用laravels count方法访问计数
$count = Model::where('status','=','1')->count();
或
此外,您还可以获取刀片文件中的所有数据和计数。
例如:
控制器中的代码
$posts = Post::all();
return view('post', compact('posts'));
你的代码在刀片文件中
{{ $posts->count() }}
最后,您可以看到您的帖子总数。使用where子句直接访问count()要比返回整个集合更快。例如,如果这是整个用户表的计数,只需对其进行计数就可以返回1m个结果,这将占用大量资源。这应该是最重要的,雄辩的一个更优雅
$count = Model::count();
$posts = Post::all();
return view('post', compact('posts'));
{{ $posts->count() }}