Laravel How To Get Max Value of Column Tutorial

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

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

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.

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