Search code examples
laravel-5.4laravel-4.2

Get weekly / monthly records in laravel 4.2 and 5+?


How do I use the created_at field to get only the records that were created last week and all the weeks of the month and monthly in laravel 4.2 and 5+ versions?


Solution

  • To fetch record which inserted within one week in laravel, simple example below..

    $7days_ago_date = date('Y-m-d', strtotime('-8 days', strtotime(date('Y-m-d'))));
    
    $Tracker = Tracker::orderby('created_at', 'desc')->whereBetween( 
    DB::raw('date(created_at)'), [$7days_ago_date, date('Y-m-d')] )->get();