Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

mysql

laravel

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?

like image 303
Curri Avatar asked Aug 05 '15 00:08

Curri


People also ask

What is raw DB?

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 is raw expression in laravel?

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.

What is DB :: unprepared in laravel?

DB::unprepared - Option to return false on failure Upon a successful entry the method return true and all works as expected.

How to retrieve all data from a database in Laravel?

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.

Can I run Raw SQL queries in Laravel query builder?

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.

How to use selectraw () eloquent query in Laravel?

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.

What can I do with it in Laravel?

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.


1 Answers

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

like image 182
Maytham Avatar answered Oct 13 '22 00:10

Maytham