laravel withCount 统计关联数量的方法

(编辑:jimmy 日期: 2024/10/4 浏览:2)

统计关联数量,单个用法

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

多个用法

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

调用

$post->comments_count

一定要是5.3版本之后,5.2和5.1都会报方法未定义

以上这篇laravel withCount 统计关联数量的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。