Laravel How To Get Min Value of Column Tutorial

Reading Time: 3 minutes
625 Views

Inside this article we will see the concept i.e Laravel How To Get Min Value of Column Tutorial. Article contains the classified information about Laravel Get Min Value Query using Laravel min(), oldest() and orderBy() methods.

If you are working with laravel logical concepts, then you see many times we need to get the minimum value of a column or the very first records. This helps to get the very first inserted row of a table.

Read More: Laravel How To Get Max Value of Column Tutorial

Let’s get started.

Laravel Installation

Open terminal and run this command to create a laravel project.

composer create-project laravel/laravel myblog

It will create a project folder with name myblog inside your local system.

To start the development server of laravel –

php artisan serve

URL: http://127.0.0.1:8000

Assuming laravel already installed inside your system.

Example #1: Get Minimum Value Using min() Method

Suppose we have a Test controller where we adding this method concept.

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use App\Models\Product;
  
class TestController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function index(Request $request)
    {
        $minID = Product::whereNotNull('price')->min("price");
  
        dd($minID);
    }
}

Concept

Using min() method you can get the min column value.

$minID = Product::whereNotNull('price')->min("price");

Read More: How To Create Arithmetic Calculator Using jQuery

Example #2: Get Minimum Value Using orderBy() Method

Suppose we have a Test controller where we adding this method concept.

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use App\Models\Product;
  
class TestController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function index(Request $request)
    {
        $minID = Product::whereNotNull('price')->orderBy('price', 'asc')->value('price');
  
        dd($minID);
    }
}

Concept

Using orderBy() method you can get the min column value.

$minID = Product::whereNotNull('price')->orderBy('price', 'asc')->value('price');

Example #3: Get Minimum Value Using oldest() Method

Suppose we have a Test controller where we adding this method concept.

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use App\Models\Product;
  
class TestController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function index(Request $request)
    {
        $minID = Product::oldest()->value('price');
  
        dd($minID);
    }
}

Concept

Using oldest() method you can get the min column value.

$minID = Product::oldest()->value('price');

We hope this article helped you to learn Laravel How To Get Min Value of Column Tutorial in a very detailed way.

Read More: How To Disable Specific Dates In jQuery Datepicker Tutorial

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.

Sanjay KumarHello friends, I am Sanjay Kumar a Web Developer by profession. Additionally I'm also a Blogger, Youtuber by Passion. I founded Online Web Tutor and Skillshike platforms. By using these platforms I am sharing the valuable knowledge of Programming, Tips and Tricks, Programming Standards and more what I have with you all. Read more