How to Get Current Year Data in Laravel?

By Hardik Savani April 16, 2024 Category : Laravel

Today, i would like to show you how to get current year data in laravel. This article will give you simple example of laravel get current year data from database. This post will give you simple example of get current year data in laravel. In this article, we will implement a laravel current year data. Let's get started with laravel current year records example.

we can get current year records in laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11 application.

Here, i will give you very simple example of how to get current year records in laravel. we will use whereYear() in this example.

I have one created items table that structure as bellow you can see.

items table:

Controller Code:

<?php

namespace App\Http\Controllers;

use Illuminate\Support\Facades\Http;

use App\Models\Item;

class ITSController extends Controller

{

/**

* Write code on Method

*

* @return response()

*/

public function index()

{

$items = Item::select('*')

->whereYear('created_at', date('Y'))

->get();

dd($items);

}

}

Output:

Array

(

[0] => Array

(

[id] => 2

[title] => Dr.

[body] => Qui omnis qui ad rerum. Cum in quia similique est. Nam beatae officiis quia consequuntur beatae. Et voluptas sapiente qui provident facere facere et.

[created_at] => 2021-08-12T13:45:17.000000Z

[updated_at] => 2021-08-12T13:45:17.000000Z

)

[1] => Array

(

[id] => 3

[title] => Mr.

[body] => Quae reprehenderit et quisquam maiores voluptatem ab. Adipisci omnis qui vitae quo corrupti quidem reiciendis. Mollitia est quia voluptatem qui optio.

[created_at] => 2021-08-12T13:45:17.000000Z

[updated_at] => 2021-08-12T13:45:17.000000Z

)

[2] => Array

(

[id] => 4

[title] => Dr.

[body] => Incidunt quia quibusdam repellendus dolore. Aut rerum totam veritatis voluptates mollitia dolor quo. Placeat ab nam magni incidunt et qui. Architecto hic vel sequi porro aliquam.

[created_at] => 2021-08-12T13:45:17.000000Z

[updated_at] => 2021-08-12T13:45:17.000000Z

)

[3] => Array

(

[id] => 5

[title] => Ms.

[body] => Minus voluptatum mollitia voluptatem ullam aperiam dolorum. Dolorem aut sed aut ipsum sint quisquam. Quia culpa doloribus perferendis ea consequuntur veniam quam. Qui aut blanditiis corporis et.

[created_at] => 2021-08-12T13:45:17.000000Z

[updated_at] => 2021-08-12T13:45:17.000000Z

)

[4] => Array

(

[id] => 6

[title] => Dr.

[body] => Eum sit sit ab non. Numquam soluta pariatur recusandae ex quo optio. Adipisci est non corporis omnis. Sit corporis est occaecati. Magnam laudantium consequatur illo quidem nemo doloremque.

[created_at] => 2021-08-12T13:45:17.000000Z

[updated_at] => 2021-08-12T13:45:17.000000Z

)

[5] => Array

(

[id] => 7

[title] => Miss

[body] => Quos rerum magni tempora non. Dolorum est laboriosam qui neque consequatur ipsa aliquid. Sit qui distinctio molestiae non qui distinctio.

[created_at] => 2021-08-12T13:46:08.000000Z

[updated_at] => 2021-08-12T13:46:08.000000Z

)

[6] => Array

(

[id] => 8

[title] => Mrs.

[body] => At aperiam reprehenderit ratione qui. Enim fugit tenetur id optio ut. Ut doloremque quibusdam libero nihil. Quia omnis soluta quia.

[created_at] => 2021-08-12T13:46:08.000000Z

[updated_at] => 2021-08-12T13:46:08.000000Z

)

[7] => Array

(

[id] => 9

[title] => Dr.

[body] => Body 4

[created_at] => 2021-10-06T18:13:40.000000Z

[updated_at] => 2021-08-12T13:46:08.000000Z

)

[8] => Array

(

[id] => 10

[title] => Dr.

[body] => Body 3

[created_at] => 2021-10-05T13:46:08.000000Z

[updated_at] => 2021-08-12T13:46:08.000000Z

)

[9] => Array

(

[id] => 11

[title] => Miss

[body] => Non et necessitatibus minima est ut velit. Et fugit vero harum omnis rerum quisquam sit autem. Ad dolor voluptas delectus cupiditate nesciunt deleniti sint.

[created_at] => 2021-08-12T13:46:08.000000Z

[updated_at] => 2021-08-12T13:46:08.000000Z

)

[10] => Array

(

[id] => 12

[title] => Dr.

[body] => Body 2

[created_at] => 2021-10-04T13:46:43.000000Z

[updated_at] => 2021-08-12T13:46:43.000000Z

)

[11] => Array

(

[id] => 13

[title] => Mrs.

[body] => Body 1

[created_at] => 2021-10-05T13:46:43.000000Z

[updated_at] => 2021-08-12T13:46:43.000000Z

)

)

I hope it can help you...

Tags :
Shares