Laravel Get Min Value of Column Example
Hi Guys,
Today, I would like to show you laravel get min value of column. If you have a question about laravel get row with min value then I will give a simple example with a solution. we will help you to give an example of get min value of a column in laravel. Here you will learn get min value from collection 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...