Laravel how to get query with bindings?

前端 未结 14 733
小蘑菇
小蘑菇 2021-01-31 15:58

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();

Mode         


        
14条回答
  •  南笙
    南笙 (楼主)
    2021-01-31 16:32

    The following function ensures the resulting SQL doesn't confuse bindings with columns by enclosing the ? to be '?'

        public static function getFinalSql($query)
        {
            $sql_str = $query->toSql();
            $bindings = $query->getBindings();
    
            $wrapped_str = str_replace('?', "'?'", $sql_str);
    
            return str_replace_array('?', $bindings, $wrapped_str);
        }
    

提交回复
热议问题