Why use DB :: raw inside DB :: select in Laravel?

Is it mandatory to use the DB::raw function when starting a query, and you are not using the free query constructor in Laravel?

eg.

 $result = DB::select("SELECT * FROM users"); $result2 = DB::select(DB::raw("SELECT * FROM users")); 

I get the same result in both cases. So why use DB::raw ?

+6
source share
1 answer

DB::raw() used to execute arbitrary SQL commands that are not processed further by the query builder. Therefore, they can create a vector to attack through SQL injection.

Check out this ref. link, with more details: http://fideloper.com/laravel-raw-queries

Example DB::raw and DB::select

+3
source

All Articles