Laravel How To Get Max Value of Column Tutorial

Reading Time: 3 minutes
673 Views

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

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

Read More: How To Create Arithmetic Calculator Using jQuery

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 Maximum Value Using max() 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\Post;
  
class TestController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function index(Request $request)
    {
        $maxID = Post::max("id");
  
        dd($maxID);
    }
}

Concept

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

$maxID = Post::max("id");

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

Example #2: Get Maximum Value Using latest() 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\Post;
  
class TestController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function index(Request $request)
    {
        $maxID = Post::latest()->value('id');
  
        dd($maxID);
    }
}

Concept

Using latest() method you can get the max column value.

$maxID = Post::latest()->value('id');

Example #3: Get Maximum 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\Post;
  
class TestController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function index(Request $request)
    {
        $maxID = Post::orderBy('id', 'desc')->value('id');
  
        dd($maxID);
    }
}

Concept

Using orderBy() method you can get the max column value by putting rows into descending order and picking the top one row.

$maxID = Post::orderBy('id', 'desc')->value('id');

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

Read More: Laravel 9 How To Encrypt and Decrypt String 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