resources/views/shared/_stats.blade.php 这个视图里,为什么微博数量的获取方式跟关注和粉丝数不一样?

<div class="stats">
  <a href="{{ route('users.followings', $user->id) }}">
    <strong id="following" class="stat">
      {{ count($user->followings) }}
    </strong>
    关注
  </a>
  <a href="{{ route('users.followers', $user->id) }}">
    <strong id="followers" class="stat">
      {{ count($user->followers) }}
    </strong>
    粉丝
  </a>
  <a href="{{ route('users.show', $user->id) }}">
    <strong id="statuses" class="stat">
      {{ $user->statuses()->count() }}
    </strong>
    微博
  </a>
</div>

这段代码当中为什么微博数量的获取方式不用前面一样的那种,count($user->statuses)呢?

《L01 基础入门》
我们将带你从零开发一个项目并部署到线上,本课程教授 Web 开发中专业、实用的技能,如 Git 工作流、Laravel Mix 前端工作流等。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
最佳答案

效果是一样的,你喜欢用哪种用哪种。前者是用 count() 函数计算,后者是用 SQL 计算。

6年前 评论
讨论数量: 3

效果是一样的,你喜欢用哪种用哪种。前者是用 count() 函数计算,后者是用 SQL 计算。

6年前 评论

二者还是有点区别,如果要统一,第三个需要改为:count($user->statuses),而非count($user->statuses())

6年前 评论

@hustnzj 为啥这样,一定要count集合吗

6年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!