Tuesday, July 22, 2025
HomeLanguagesLaravel Eloquent whereRaw Query Example

Laravel Eloquent whereRaw Query Example

Laravel where raw query example, you will learn how to write where raw and where DB raw query in laravel.

You can use the laravel whereRaw eloquent method to building query in laravel apps. And also use laravel where raw with multiple conditions in eloquent queries.

Follow the below examples and learn how to write query using whereRaw in laravel:

Example 1: Laravel whereRaw Query using Model

public function index()
{
    $users = User::whereRaw('YEAR(created_at) = ?', [date('Y')])->get();

    dd($users);
}

When you dump the above given whereRaw query you will get the following SQL query:

select * from `users` where YEAR(created_at) = ?

Example 2: whereRaw Query using Query Builder

public function index()
{
    $users = DB::table('users')->whereRaw('YEAR(created_at) == ? AND MONTH(created_at) == ?', [date('Y'),date('m')])->get();
                
    dd($users);
}

When you dump the above given whereRaw query you will get the following SQL query:

select * from `users` where YEAR(created_at) == ? AND MONTH(created_at) == ?
RELATED ARTICLES

Most Popular

Dominic
32158 POSTS0 COMMENTS
Milvus
67 POSTS0 COMMENTS
Nango Kala
6530 POSTS0 COMMENTS
Nicole Veronica
11681 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11742 POSTS0 COMMENTS
Shaida Kate Naidoo
6625 POSTS0 COMMENTS
Ted Musemwa
6907 POSTS0 COMMENTS
Thapelo Manthata
6592 POSTS0 COMMENTS
Umr Jansen
6588 POSTS0 COMMENTS