Laravel Eloquent whereYear() Query Example

Laravel whereYear query example; In this tutorial, we are going to share how to use whereYear query builder in laravel. The whereYear method may be used to compare a column’s value against a specific year.

If you want to get a specific year records in laravel then you just use the whereYear query. You can use it in your laravel 5, laravel 6, laravel 7, laravel 8 or laravel 9 apps.

Example 1:

use App\Models\User;

class UserController extends Controller
{
    public function index()
    {
        $year = '2020';
        $users = User::whereYear('created_at', '=', $year)
                    ->get();
        dd($users);
    }
}

Example 2:

use App\Models\User;

class UserController extends Controller
{
    public function index()
    {
        $year = '2020';
        $users = User::whereYear('created_at', '>=', $year)
                    ->get();
        dd($users);
    }
}

I hope this will help you..

Leave a Comment