laravel withCount 统计关联数量的方法

时间:2022-07-27
本文章向大家介绍laravel withCount 统计关联数量的方法,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

统计关联数量,单个用法

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