How to Get Min Value of Column in Laravel?
Jan 11, 2023
Hi Guys,
I'd like to demonstrate how to find the minimum value of a column in Laravel today. I'll present a straightforward example and provide a solution if you have a question regarding laravel get row with min value. We will assist you by providing a sample of how to obtain a column's minimum value in Laravel. You will discover how to obtain the minimum value from a collection using Laravel in this article.
In Laravel, there are several ways to obtain the minimal id. I'll give you three examples utilising the orderBy(), oldest(), and min() methods. So, let's look at the sample one by one.
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
- How to Notifications With database Driver in Laravel 11?
- How to Send Email Via Notification in Laravel 11?
- How to Install and Configure Laravel 11 Debugbar?
- How to Like Dislike System in Laravel 11?
- How to Paginate a Model with Relationship in Laravel 11?
- Laravel 11 Upload Files to Amazon S3 Example
- Laravel 11 Send WhatsApp Messages Example
- Laravel 11 Rest API Authentication Using JWT Tutorial