当前位置 主页 > 网站技术 > 代码类 >

    laravel withCount 统计关联数量的方法

    栏目:代码类 时间:2019-10-22 18:04

    统计关联数量,单个用法

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