Laravel Collections | Calculating Median Using Collections

Hello Artisan

In this tutorial i will show you how we can find median value from a sequence of series in laravel using laravel collection median method. If you don't know about laravel collection method, then you are a right place. I will teach you from beginner to advance.

Simply median is the middle value from a sequence of number. How we can find median in laravel? do you know? We can find it easily using laravel collection median method. So let's see how to find median in laravel.

Route::get('/', function () {

    $data = [
        10,
        20,
        30
    ];

   return collect($data)->median();
 
 });

Here output will 20. Because 20 is the middle value of this series.  Let's see another example with parameter.

Route::get('/', function () {

    $data = [
        ["price" => 10],
        ["price" => 20],
        ["price" => 30]
    ];

    return collect($data)->median("price"); 
 
});

Here also we will get the exact same output like before. Hope it can help you.

 

A web enthusiastic, self-motivated Full-Stack Web Developer from Dhaka, Bangladesh with experience in developing applications using JavaScript, Laravel & Wordpress specifically. Facebook Github Website