本文介绍了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?
推荐答案
查看 getBindings() Builder
类
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如何获取带有绑定的查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!