Laravel中如何使用withCount方法来统计关联数据的数量?

2026-06-09 19:571阅读0评论SEO问题
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计177个文字,预计阅读时间需要1分钟。

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。

统计关联数量,单个用法

$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方法来统计关联数据的数量?

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

标签:

本文共计177个文字,预计阅读时间需要1分钟。

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。

统计关联数量,单个用法

$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方法来统计关联数据的数量?

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

标签: