Laravel how to get query with bindings?

前端 未结 14 714
小蘑菇
小蘑菇 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:37

    Building upon Douglas.Sesar's answer.

    I found I also needed to put the bindings in single quotations to be able to easily paste it into my database IDE.

    $sql = $query->toSql();
    $bindings = $query->getBindings();
    
    $sql_with_bindings = preg_replace_callback('/\?/', function ($match) use ($sql, &$bindings) {
        return "'" . array_shift($bindings) . "'";
    }, $sql);
    

提交回复
热议问题