Using Aggregates function with Laravel Eloquent, Query Builder


Image title

Once you have created a Eloquent model and its associated database table, you can start retrieving data from your database. Think of each Eloquent model as a powerful query builder allowing you to fluently query the database table associated with the model.

Of course, in addition to retrieving models, you may use count,min,max,sum and avg aggregate function provided by Laravel to get the appropriate scalar value instead of a full model instance. You may call any of these methods after constructing your query:


<?php

use App\Product;

$count = Product::where('active', 1)->count();
$max = Product::where('active', 1)->max('price');
$min = Product::where('active', 1)->min('price');
$sum = Product::where('active', 1)->sum('price');
$avg = Product::where('active', 1)->avg('price');


If you have any other questions, experience or insights on "Using Aggregates function with Laravel Eloquent, Query Builder" please feel free to leave your thoughts in the comments bellow which might be helpful to someone!

Written by Akram Wahid 6 years ago

are you looking for a chief cook who can well craft laravel and vuejs, to make some awsome butterscotch,
yes then it is right time for you to look at my profile.

Do you want to write Response or Comment?

You must be a member of techalyst to proceed!

Continue with your Email ? Sign up / log in

Responses

Be the first one to write a response :(

{{ item.member.name }} - {{ item.created_at_human_readable }}

{{ reply.member.name }} - {{ reply.created_at_human_readable }}