Laravel查询生成器,选择原始或选择并原始 [英] Laravel Query Builder, selectRaw or select and raw
本文介绍了Laravel查询生成器,选择原始或选择并原始的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
之间有什么区别
DB::table('some_table')
->selectRaw('COUNT(*) AS result')
->get();
和:
DB::select(DB::raw("
SELECT COUNT(*) AS result
FROM some_table"));
他们在文档 https://laravel.com/docs/5.6/queries 中进行广告关于使用raw()
进行SQL注入,但是与selectRaw
相同吗?
In the documentation https://laravel.com/docs/5.6/queries they advert about using raw()
due SQL Injection, but it's the same with selectRaw
?
推荐答案
两者的最终结果相同,即存在一些差异:
The end result of both is the same i.e but there are some difference:
第一个:
DB::table('some_table')
->selectRaw('COUNT(*) AS result')
->get();
- 返回PHP对象的集合,
- 您可以根据结果流畅地调用collections方法
- 比较干净.
而第二个:
DB::select(DB::raw("
SELECT COUNT(*) AS result
FROM some_table"
));
- 返回Php对象的数组.
- Returns an array of Php object.
尽管它们有相似之处:原始查询字符串.
Although they have similarities: the raw query string.
这篇关于Laravel查询生成器,选择原始或选择并原始的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文