How to Get Current Month Records in Laravel

  518 views   6 months ago Laravel

You may want to add filter while fetching records. This basic functionality requires where showing list. For example, transactions, report etc.

In this article, I will share you how you can fetch records for the current month using Laravel eloquent and query builder. This example can be used in Laravel 6, Laravel 7 or Laravel 8 application.

Let's import some dummy data into database using following SQL query.

CREATE TABLE `articles` (
  `id` bigint UNSIGNED NOT NULL,
  `title` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
  `body` text COLLATE utf8mb4_unicode_ci,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

INSERT INTO `articles` (`id`, `title`, `body`, `created_at`, `updated_at`) VALUES
(1, 'This is sample title.', 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod\r\ntempor incididunt ut labore et dolore magna aliqua.', '2021-09-21 07:08:48', '2021-09-21 07:08:48'),
(2, 'This is sample another title.', 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod\r\ntempor incididunt ut labore et dolore magna aliqua.', '2021-09-27 07:09:06', '2021-09-27 07:09:12'),
(3, 'This is sample title.', 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod\r\ntempor incididunt ut labore et dolore magna aliqua.', '2021-10-12 07:09:16', '2021-10-12 07:09:19'),
(4, 'This is extra sample title.', 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod\r\ntempor incididunt ut labore et dolore magna aliqua.', '2021-10-15 07:09:22', '2021-10-14 07:09:26'),
(5, 'This is sample title.', 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod\r\ntempor incididunt ut labore et dolore magna aliqua.', '2021-10-15 07:09:29', '2021-10-15 07:09:33'),
(6, 'Watch out this text.', 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod\r\ntempor incididunt ut labore et dolore magna aliqua.', '2021-10-22 07:09:49', '2021-10-22 07:09:40'),
(7, 'Find out the current title', 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod\r\ntempor incididunt ut labore et dolore magna aliqua.', '2021-10-23 07:10:44', '2021-10-23 07:10:48');

Let's assume that the current month October has 5 records and Septermber has first 2 records.

Laravel whereMonth() method

/**
 * Display a listing of the resource.
 *
 * @return \Illuminate\Http\Response
 */
public function index()
{
    $articles = Article::whereMonth('created_at', \Carbon\Carbon::now()->month)
            ->whereYear('created_at', \Carbon\Carbon::now()->year)
            ->get();

    dd($articles);
}

Now let's look an example using Laravel query builder. We will also use SQL MONTH function with PHP date() function.

/**
 * Display a listing of the resource.
 *
 * @return \Illuminate\Http\Response
 */
public function index()
{
    $articles = \DB::table('articles')
            ->whereRaw('MONTH(created_at) = ?',[date('m')])
            ->whereRaw('YEAR(created_at) = ?',[date('Y')])
            ->get();

    dd($articles);
}

This will return the current month records with same year. I hope it will help you.

Author : Harsukh Makwana
Harsukh Makwana

Hi, My name is Harsukh Makwana. i have been work with many programming language like php, python, javascript, node, react, anguler, etc.. since last 5 year. if you have any issue or want me hire then contact me on [email protected]