Where x whereRaw What’s the difference?

Asked

Viewed 280 times

3

I tried to read Laravel’s documentation, but I didn’t really understand the difference. I also tried to search for other links but, I usually see topics in forums asking for help on the use and there is no clear explanation about the reason for the use.

After all, what is the whereRaw and when to use instead of using Where?

1 answer

4


The whereRaw() is a function of the Laravel Builder query to put the input as it is in the clause where.

It’s basically a function where() whose arguments will not be processed before they are entered in the query.

Ex:

That query:

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

Will be converted into:

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

While this query:

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

will be converted into:

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.