Laravel Gets the Average from a Column with Code Examples

Nov 14, 2022 . Admin

Hello Friends,

Today our leading topic is laravel gets the average from a column with code examples. I want to share with you that laravel gets the average from a column with code examples. this example will help you calculate column averages in the laravel collection. I want to share with you the laravel collection calculates column averages. Alright, let’s dive into the steps.

You can use this example with the versions of laravel 6, laravel 7, laravel 8, and laravel 9.

You have just to follow the below step and you will get the layout as below:

Step 1: Install Laravel

This is optional; however, if you have not created the laravel app, then you may go ahead and execute the below command:

composer create-project laravel/laravel example-app
Step 2: Create Format Route web.php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\FindAvgController;

| Web Routes
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!

Route::get('/index',[FindAvgController::class, 'index']);
Step 3: create FindAvgController

we will create FindAvgController. so you can see the below code with output:

php artisan make:controller FindAvgController

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class FindAvgController extends Controller
     * The attributes that are mass assignable.
     * @var array
    public function index()
        $data = [
            ["price" => 35],
            ["price" => 16],
            ["price" => 47]

Step 4: Start Development Server

Start the development server. Use the PHP artisan serve command and start your server:

php artisan serve

Now you are ready to run our example so run the below command to quick run.

32.666666666667 // app/Http/Controllers/FindAvgController.php:22

I hope it can help you...