roleModel定义关联 hasmany 

public function users(){ return $this->hasMany('App\Models\Users', 'role_id', 'role_id'); }

return $this->query()->withCount("users")->get();

  

  

 

统计关联数量,单个用法

$posts = Post::withCount('comments')->orderBy('id','desc')->paginate(6);

多个用法

$posts = Post::withCount(['comments','zans'])->orderBy('id','desc')->paginate(6);

调用

$post->comments_count

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-05-12
  • 2021-10-28
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-28
  • 2021-12-04
  • 2021-11-29
  • 2022-12-23
相关资源
相似解决方案