php 为什么在 Laravel 中的 DB::select 中使用 DB::raw?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31821421/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 22:38:30  来源:igfitidea点击:

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

phpmysqllaravel

提问by Curri

Is it mandatory to use the function DB::rawwhen you are running a query and you are not using the fluent query builder in Laravel?

DB::raw当您运行查询并且您没有在 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?

我在两种情况下都得到相同的结果。那么为什么需要使用DB::raw呢?

采纳答案by maytham-???????

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 attackvia SQL injection.

DB::raw()用于生成查询构建器不会进一步解析的任意 SQL 命令。因此,他们可以通过 SQL 注入创建攻击向量

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

检查这个参考。链接,更多细节:http: //fideloper.com/laravel-raw-queries

Exampleof DB::rawand DB::select

实施例DB::rawDB::select