Laravel 如何使用绑定获取查询? [英] Laravel how to get query with bindings?
本文介绍了Laravel 如何使用绑定获取查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一些查询需要使用查询构建器传递给另一个查询
I have some query that I need to pass to another query using query builder
$query = DB::table('table')->whereIn('some_field', [1,2,30])->toSql();
Model::join(DB::raw("({$query}) as table"), function($join) {
$join->on('model.id', '=', 'table.id');
})
结果应该是
Select * from model join (select * from table where some_field in (1,2,30)) as table on model.id = table.id
但是绑定没有通过,这迫使我去做
but the bindings are not passed, which force me to do
$query = DB::table('table')->whereRaw('some_field in ('. join(',', [1,2,30]) .')')->toSql();
有时可能不安全.如何获取带有绑定的查询?
what can be unsafe at times. How can I get the query with bindings?
推荐答案
查看 Builder
类上的 getBindings()
方法
Check out the getBindings()
method on the Builder
class
$query = DB::table('table')->whereIn('some_field', [1,2,30]);
$sql = $query->toSql();
$bindings = $query->getBindings();
这篇关于Laravel 如何使用绑定获取查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文