How to get count of all records created at yesterday

You can get count of all records created at yesterday. You can get the total number of all records by using the count method after the where condition which helps you to filter the data from table which are created at yesterday

Answers 1
  • Get count of yesterday created records in Laravel

    $count = App\Models\User::whereDate('created_at', '=', \Carbon\Carbon::yesterday()
        ->format('Y-m-d'))->count();
    
    //Demo code with output:
    Route::get('/get-count-of-records', function(){
        $count = App\Models\User::whereDate('created_at', '=', \Carbon\Carbon::yesterday()
        ->format('Y-m-d'))->count();
        return $count;
    });
    
    0

    Output:

    2

    You can simply copy/paste code in routes\web.php and visit URL (/get-count-of-records) to get the output. This code snippet will help you to get the count of all records which are created at yesterday in Laravel. Output may be different as per your database table records. It will return 0 if there is no record exists which is created at yesterday.

     

  • Back to code snippet queries related laravel