狗屁昵称 发表于 2023-9-14 12:29:57

雄辩的laravel:如何从-> get()获取行数

当我发现如何使用这一集的数行时,我遇到了很多麻烦。
$wordlist = \DB::table('wordlist')->where('id','get();我试过,adding->count()但是没用。我试过。count($wordlist)。我不确定没有第二个请求。a->count()如何做方法。
                                                               
    解决方案:                                                               
                                                                答案已更新
count是一个Collection方法。查询构建器返回数组。因此,为了获得计数,您只需要像通常使用数组一样计数:
$wordCount = count($wordlist);如果有单词列表模型,可以使用Eloquent来获取Collection,然后使用Collection的count例子:
$wordlist = Wordlist::where('id','get();$wordCount = $wordlist->count();这里有/正在讨论让查询生成器返回一个集合:https
:
//github.com/laravel/framework/issues/10478
但到目前为止,查询生成器总是返回一个数组。
编辑:以上链接,查询生成器现在返回集合(不是数组)。JP Foster最初尝试执行的操作将起作用:
$wordlist = \DB::table('wordlist')->where('id','get();$wordCount = $wordlist->count();但是,正如Leon评论中指出,如果只需要计数,直接查询比获得整个集合和计数要快得多。换句话说,您可以执行以下操作:
// Query builder$wordCount = \DB::table('wordlist')->where('id','count();// Eloquent$wordCount = Wordlist::where('id','count();;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
页: [1]
查看完整版本: 雄辩的laravel:如何从-> get()获取行数