Laravel How To Get Min Value of Column Tutorial

Share this Article
Reading Time: 3 minutes
178 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.

Learn More –

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");

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.

Buy Me a Coffee

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.

Was this post helpful?

Get PHP Web Development Courses on Offer in just ₹299
Get PHP Web Development Courses on Offer in just ₹299