Laravel Get Record Last 7,15,30 Days, Week, Month, Year

Get last 1, 3, 6,12 months & year, and get the last 7,15,30 days data in laravel; Through this tutorial, you will learn how to get the last 1, 3, 6, 12 months, the last date, last week, last, month, last year data records in laravel.

When you work with any laravel eCommerce application, dating application, etc. that time we need to show analytics in the application dashboard.

This laravel tutorial, explains how to fetch last date/day, week, month, year, e.g. Day/Date wise, last week wise data, get month wise last year data, Last year records from the database.

Table Of Content

  • Laravel Get Last Day Records
  • Get Last Week Data in Laravel
  • Laravel Get Last Month Records
  • Get Last 15 Days & 30 Days Records in Laravel
  • Fetch Month Wise Last Year Data
  • Fetch Last Year Record

Laravel Get Last Day Records

In laravel, you want to get last date record from database tables. So you can use below laravel eloquent query to fetching the last date/day records.

User::where('created_at','>=',Carbon::now()->subdays(1))->get(['name','created_at']);

The output of the above laravel eloquent query looks like:

Array
(
    [0] => Array
        (
            [name] => Денис Борисов
            [created_at] => 2019-12-10 11:23:14
        )
    [1] => Array
        (
            [name] => tt
            [created_at] => 2019-12-10 12:07:19
        )
    [2] => Array
        (
            [name] => ajay
            [created_at] => 2019-12-10 12:26:25
        )
    [3] => Array
        (
            [name] => Ramakrishna P
            [created_at] => 2019-12-10 12:50:06
        )
)        

Get Last Week Data in Laravel

How to get last week data in laravel?

You can use the laravel eloquent method whereBetween to get the last week records from the database table as follow:

 $previous_week = strtotime("-1 week +1 day");
 $start_week = strtotime("last sunday midnight",$previous_week);
 $end_week = strtotime("next saturday",$start_week);
 $start_week = date("Y-m-d",$start_week);
 $end_week = date("Y-m-d",$end_week);

 User::whereBetween('created_at', [$start_week, $end_week])->get(['name','created_at']);

The output of the above laravel eloquent query looks like:

Array
(
    [0] => Array
        (
            [name] => 1
            [created_at] => 2019-12-01 01:07:28
        )
    [1] => Array
        (
            [name] => 2
            [created_at] => 2019-12-03 01:07:38
        )
    [2] => Array
        (
            [name] => 3
            [created_at] => 2019-12-04 01:07:46
        )
    [3] => Array
        (
            [name] => 4
            [created_at] => 2019-12-06 01:07:52
        )
    [4] => Array
        (
            [name] => 5
            [created_at] => 2019-12-07 01:07:57
        )
 )       

Laravel Get Last Month records

If you want to get or fetch previous month records in laravel. Use the below laravel eloquent query to get the last month records from the database table.

User::whereMonth('created_at', '=', Carbon::now()->subMonth()->month)->get(['name','created_at']);

This query uses laravel method whereMonth() and get().

The output of the above laravel Eloquent query look like:

Array
(
    [0] => Array
        (
            [name] => d
            [created_at] => 2019-11-01 02:03:51
        )
    [1] => Array
        (
            [name] => vichhai
            [created_at] => 2019-11-05 02:52:43
        )
    [2] => Array
        (
            [name] => Dalang Ly
            [created_at] => 2019-11-20 03:06:20
        )
    [3] => Array
        (
            [name] => ssa
            [created_at] => 2019-11-25 04:03:42
        )
    [4] => Array
        (
            [name] => Razeek
            [created_at] => 2019-11-30 04:57:53
        )
)

Get Last 15 Days & 30 Days Records in Laravel

If You want to get the last 15 days and last 30 days records from the database in laravel. Use the below given laravel eloquent query:

$last_15_days = User::where('created_at','>=',Carbon::now()->subdays(15))->get(['name','created_at']);

 $last_30_days = User::where('created_at','>=',Carbon::now()->subdays(30))->get(['name','created_at']);

Laravel Get Last Year Record

If You want to get the previous year records from the database in laravel. Use the below-given laravel eloquent query:

User::whereYear('created_at', date('Y', strtotime('-1 year')))->get(['name','created_at']);

This query users laravel method whereYear() and get() to fetch records from db.

The output of above laravel eloquent query looks like:

Array
(
    [0] => Array
        (
            [name] => James
            [created_at] => 2018-03-07 21:18:59
        )
    [1] => Array
        (
            [name] => Maaf Kan Lah
            [created_at] => 2018-03-07 21:18:59
        )
    [2] => Array
        (
            [name] => Helloo
            [created_at] => 2018-03-07 21:18:59
        )
    [3] => Array
        (
            [name] => hiiiiii888
            [created_at] => 2018-03-07 21:18:59
        )
    [4] => Array
        (
            [name] => Hellogggggg123
            [created_at] => 2018-03-07 21:18:59
        )
    [5] => Array
        (
            [name] => Df
            [created_at] => 2018-03-07 21:18:59
        )
)

Fetch Month Wise Last Year Data

When you show last year data analytics in your laravel application. You can use the laravel db::raw(), whereYear() and groupBy() to get previous year data month wise from the database table as follow:

User::select(DB::raw("(COUNT(*)) as count"),DB::raw("MONTHNAME(created_at) as monthname"))
 ->whereYear('created_at', date('Y', strtotime('-1 year')))
 ->groupBy('monthname')
 ->get();

The output of the above laravel eloquent query looks like

Array
(
    [0] => Array
        (
            [count] => 6
            [monthname] => March
        )
)

Recommended Laravel Tutorials

Recommended:-Laravel Try Catch

AuthorDevendra Dode

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

Your email address will not be published. Required fields are marked *