Laravel Get Min Value of Column Code Example

Jan 11, 2023 . Admin



Hi Guys,

Today our leading topic is laravel get min value of column code example. I would like to show you laravel get min value of column. I’m going to show you about laravel get row with min value. This tutorial will give you simple example of get min value of a column in laravel. you will do the following things for how to get min id in laravel.

There are several ways to get get minimum id in laravel. i will give you three example using min(), oldest() and orderBy() method. so, let's see the one-by-one example.

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

I hope it can help you...

#Laravel