Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 雄辩的laravel:如何从a->;中获取行计数;得到()_Php_Sql_Laravel_Eloquent - Fatal编程技术网

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() }}