How to Create Search Date Range Query in Laravel
Let's assume that we have a Laravel e-commerce website and we want to fetch sales between a range of dates, so how we can do that?
Fetch data between two given dates
You can use this Laravel eloquent query to fetch data between two given dates:
<?php
namespace App\Http\Controllers;
use Carbon\Carbon;
use App\Models\Order;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
class SalesController extends Controller
{
//
public function searchByRangeOfDates($from, $to){
$orders = Order::whereBetween('created_at', [$from, $to])
->latest()->get();
return view('sales.index')->with([
'orders' => $orders,
]);
}
}