Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is meant by whereRaw in PHP Laravel framework

Tags:

php

laravel

I'm unsure what whereRaw is in PHP Laravel framework. Could you provide good and easily understandable example, please?

like image 518
kavi krish Avatar asked Mar 21 '17 12:03

kavi krish


1 Answers

WhereRaw() is a function of Laravel query builder which puts your input as it is in the SQL query's where clause.

Think of it as the where() function whose input argument will not be processed before inserting into queries.

See the example below:

$Query = DB::table('some_table')->where('YEAR(date)', 'YEAR(CURRENT_DATE)');

In this Laravel will resolve your arguments to build a query. Which will result in the following query because your input will be treated as some field and its its value :

SELECT * FROM `some_table` WHERE `YEAR(date)` = `YEAR(CURRENT_DATE)`

Which is not desired.

And now if you use whereRaw like:

$Query = DB::table('some_table')->whereRaw('YEAR(date) = YEAR(CURRENT_DATE)');

Now Laravel put this where clause as it is in your query, like below:

SELECT * FROM `some_table` WHERE YEAR(date) = YEAR(CURRENT_DATE)

Hope it helped (:

like image 167
Parantap Parashar Avatar answered Sep 18 '22 18:09

Parantap Parashar