I have a table where daily sales of products is recorded. The products which are not sold for a particular date is not recorded in the table. But in the blade page, I have to list out all the dates within a particular range of date selected by the user. For certain dates, where sales is zero, it should display as zero.
==========================================
product || total_sales || date
A || 500 || 2025-07-01
B || 800 || 2025-07-01
A || 200 || 2025-07-02
B || 250 || 2025-07-02
A || 500 || 2025-07-04
B || 780 || 2025-07-04
A || 180 || 2025-07-05
A || 615 || 2025-07-06
B || 756 || 2025-07-06
==========================================
Now if the user selects the date range between 07-01 to 07-05, then the output should be shown as::
==========================================
product || total_sales || date
A || 500 || 2025-07-01
B || 800 || 2025-07-01
A || 200 || 2025-07-02
B || 250 || 2025-07-02
A || 0 || 2025-07-03
B || 0 || 2025-07-03
A || 500 || 2025-07-04
B || 780 || 2025-07-04
A || 180 || 2025-07-05
B || 0 || 2025-07-05
==========================================
How can I do it?
Controller class::
public function index()
{
try {
$from = date('2025/07/01');
$to = date('2025/07/05');
$sales = Sales::whereBetween('sales_date_eng',[$from, $to])->get();
return view('admin.sales', compact('sales'));
} catch (Exception $exception) {
return response()->json(['status'=>'error', 'error'=> $exception->getMessage()]);
}
}
How to display such data using laravel?