DEV Community

loading...

Eloquent Tips: withMax, withMin, withAvg & withSum.

xinecraft profile image Xinecraft ・1 min read

TIL: Apart from withCount, eloquent also have other aggregate functions like withMax, withMin, withAvg and withSum.

Eg:

$posts = Post::withSum('comments', 'votes')->get();

foreach ($posts as $post) {
    echo $post->comments_sum_votes;
}
Enter fullscreen mode Exit fullscreen mode

Discussion (0)

pic
Editor guide