Is it mandatory to use the function DB::raw
when you are running a query and you are not using the fluent query builder in Laravel?
e.g.
$result = DB::select("SELECT * FROM users");
$result2 = DB::select(DB::raw("SELECT * FROM users"));
I get the same result in both cases. So why is it necessary to use DB::raw
?
Raw data (sometimes called source data, atomic data or primary data) is data that has not been processed for use. A distinction is sometimes made between data and information to the effect that information is the end product of data processing.
What are Raw Expressions? Raw Expressions allow you to tell the query builder that you want to use what you entered and not get processed or manipulate before running the query against the database. In Laravel 4.2 I used them for subselects. They come in 2 forms, on the DB facade or an Expression class.
DB::unprepared - Option to return false on failure Upon a successful entry the method return true and all works as expected.
For retrieve all data from the database table, you can use the following query: For delete data from the database table, you can use the following query: When you write query in your controller using model or query builder and may need to use a raw expression in a query. How to write db raw query with laravel joins.
Here’s an example from official Laravel documentation: So, the point is that Laravel Query Builder has a lot of helpful methods, but it doesn’t restrict you from running raw SQL queries (or their parts) when needed.
You can use the laravel selectRaw eloquent method to building query in laravel apps. And also use laravel select raw with multiple conditions in eloquent queries. So, let’s see following examples that will help you how to use selectRaw () eloquent query in laravel: * Display a listing of the resource.
It can be used to perform most database operations in your application and works perfectly with all of Laravel's supported database systems. The Laravel query builder uses PDO parameter binding to protect your application against SQL injection attacks.
DB::raw()
is used to make arbitrary SQL commands which aren't parsed any further by the query builder. They therefore can create a vector for attack via SQL injection.
Check this ref. link, with more details: http://fideloper.com/laravel-raw-queries
Example of DB::raw
and DB::select
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With