Laravel has a strong query builder to build queries. It is as much powerful as we need. But sometimes we need to execute raw sql using Laravel. Below is a code snippet to execute raw sql queries using Laravel. The below code will return some results.
If you dont need to return results and only need to do some statement level queries then do the following:
$results = DB::select( DB::raw("SELECT * FROM table_name WHERE field_id = :field_id"), array( 'field_id' => 5, ));
If you dont need to return results and only need to do some statement level queries then do the following:
DB::statement("UPDATE USER SET STATUS='ACTIVE' WHERE ID=1;");
You can get Raw SQL and Bindings from Laravel eloquent query builder:
$query = DB::table("my_table")->where("id", $id);
echo $query->toSql();
print_r($query->getBindings());