DEV Community

Xinecraft
Xinecraft

Posted on • Edited on

Laravel Tips (Eloquent): withMax, withMin, withAvg & withSum.

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

Top comments (0)