List some Aggregates methods provided by query builder in Laravel ?
List some Aggregates methods provided by query builder in Laravel ?
Laravel's query builder provides a variety of aggregate methods that allow you to perform calculations on your database records. Here are some of the key aggregate methods available:
count(): This method returns the number of records that match the query.
$userCount = DB::table('users')->count();
max(): This method retrieves the maximum value of a specified column.
$maxPrice = DB::table('orders')->max('price');
min(): This method retrieves the minimum value of a specified column.
$minPrice = DB::table('orders')->min('price');
avg() or average(): These methods calculate the average value of a specified column.
$averagePrice = DB::table('orders')->avg('price');
sum(): This method calculates the sum of a specified column.
$totalPrice = DB::table('orders')->sum('price');
These methods can be used directly on the query builder instance and can be combined with other query constraints to fine-tune the results. For example, you can use the where
clause to filter records before applying an aggregate function:
$activeUserCount = DB::table('users')->where('active', 1)->count();
$maxPrice = DB::table(...
middle
Gợi ý câu hỏi phỏng vấn
Chưa có bình luận nào