Get 30 days older records from table in laravel

Sometimes you have to get the 30 days older records from table in laravel. In the case, this code snippet will help you to get the 30 days older records from the current date from a table.

Answers 2
  • Laravel eloquent to get 30 days older records from today's date

    //1. WhereRaw() Eloquent Query:
    $users = \App\Models\User::whereRaw('DATEDIFF(NOW(), created_at) > 30')->get();
    dd($users);
    
    //2. whereDate() Eloquent Query:
    $users = \App\Models\User::whereDate('created_at', '<=', now()->subDays(30))->get();
    dd($users);
    
    1

    This code snippet will helps you to get all the records which are 30 days older from today's date and return the records as output. You can use any one of the provided code snippet to get all the records which are 30 days older from current date.

  • Get 30 days older records from current date using whereBetween method in Laravel

    $startDate = Carbon\Carbon::now()->subDays(30);
    $endDate = Carbon\Carbon::now();
    $users = App\Models\User::select("*")->whereBetween('created_at', [$startDate, $endDate])->get();
    dd($users);
    
    1

    This code snippet will help you to get the records from users table which are created between current date and created 30 days before of current date.

  • Back to code snippet queries related laravel