首页 > 编程 > PHP > 正文

laravel withCount 统计关联数量的方法

2019-11-21 19:47:38
字体:
来源:转载
供稿:网友

统计关联数量,单个用法

$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 统计关联数量的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持武林网。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表