How to Get Yesterday's Records in Laravel
In this lesson, we will see how to retrieve only yesterday's records from the database in Laravel, let's assume that we have an e-commerce website and we want to get the orders done yesterday.
Get yesterday's orders
So to get yesterday's orders we use the following eloquent model query:
<?php
namespace App\Http\Controllers\Admin;
use Carbon\Carbon;
use App\Models\Order;
use App\Http\Controllers\Controller;
class AdminController extends Controller
{
/**
* Get yesterday's orders
*/
public function index()
{
return view('admin.index')->with([
'yesterdayOrders' => Order::whereDay('created_at', Carbon::yesterday())->get()
]);
}
}