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...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- Laravel Copy Record using Eloquent Replicate Example
- Laravel Eloquent Sum Multiple Columns Example
- Laravel Eloquent firstOr() Example
- Laravel Eloquent inRandomOrder() Method Example
- Laravel Eloquent whereNull() Query Example
- Laravel Eloquent selectRaw() Query Example
- Laravel Eloquent Order By Query Example
- Laravel Eloquent whereRaw Condition Example
- Multiple orWhere Condition in Laravel Eloquent
- Laravel Collection Merge | How to Merge Two Eloquent Collection?
- Laravel One to One Eloquent Relationship Tutorial
- How to Get Query Log in Laravel Eloquent?