Câu hỏi phỏng vấn Laravel
Câu hỏi

List some Aggregates methods provided by query builder in Laravel ?

Câu trả lời

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:

  1. count(): This method returns the number of records that match the query.

    php Copy
    $userCount = DB::table('users')->count();
  2. max(): This method retrieves the maximum value of a specified column.

    php Copy
    $maxPrice = DB::table('orders')->max('price');
  3. min(): This method retrieves the minimum value of a specified column.

    php Copy
    $minPrice = DB::table('orders')->min('price');
  4. avg() or average(): These methods calculate the average value of a specified column.

    php Copy
    $averagePrice = DB::table('orders')->avg('price');
  5. sum(): This method calculates the sum of a specified column.

    php Copy
    $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:

php Copy
$activeUserCount = DB::table('users')->where('active', 1)->count();
$maxPrice = DB::table(...
middle

middle

Gợi ý câu hỏi phỏng vấn

junior

What is the purpose of the Eloquent cursor() method in Laravel ?

expert

What are some Differences and Similarities Between Lumen and Laravel?

middle

What are query scopes?

Bình luận

Chưa có bình luận nào

Chưa có bình luận nào