Laravel 7/6 Eloquent whereRaw Example

Laravel 6 , Laravel , Laravel 7

Nicesnippets

04-02-2020


Here, i will show you laravel db raw where clause. step by step explain laravel whereraw example. if you want to see example of laravel whereraw date_format then you are a right place Here you will learn laravel eloquent whereraw. Follow bellow tutorial step of laravel eloquent where db raw.

I will you get to current year and month data in example.you can simply use the whereraw in example.

First of all we will example using Whereraw(), so let's see bellow example:

Example 1:


SQL Query:

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

Laravel Query:

public function index()

{

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

dd($users);

}

Example 2:

SQL Query:

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

Laravel Query:

public function index()

{

$users = User::whereRaw('YEAR(created_at) == ? AND MONTH(created_at) == ?', [date('Y'),date('m')])->get();

dd($users);

}

It will help you...


Recommended Posts