Laravel how to get query with bindings?

前端 未结 14 702
小蘑菇
小蘑菇 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:31
    public static function getQueries(Builder $builder)
    {
        $addSlashes = str_replace('?', "'?'", $builder->toSql());
        return vsprintf(str_replace('?', '%s', $addSlashes), $builder->getBindings());
    }
    
    0 讨论(0)
  • 2021-01-31 16:31

    You can define below code block as helper function and use wherever required. It will bind numeric as well as string value with quotations.

    public static function getSqlWithBindings($query)
    {
        return vsprintf(str_replace('?', '%s', $query->toSql()), collect($query->getBindings())->map(function ($binding) {
            return is_numeric($binding) ? $binding : "'{$binding}'";
        })->toArray());
    }
    

    Example:

    $query = Document::where('model', 'contact')->where('model_id', '1');
    dd(Document::getSqlWithBindings($query));
    

    Output:

    "select * from `document` where `model` = 'contact' and `model_id` = 1"
    
    0 讨论(0)
  • 2021-01-31 16:32

    If you want to get an executed query including bindings from the query log:

    \DB::enableQueryLog();
    \DB::table('table')->get();
    dd(str_replace_array('?', \DB::getQueryLog()[0]['bindings'], 
          \DB::getQueryLog()[0]['query']));
    
    0 讨论(0)
  • 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);
        }
    
    0 讨论(0)
  • 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);
    
    0 讨论(0)
  • 2021-01-31 16:39

    It is all explained here..... https://ajcastro29.blogspot.com/2017/11/laravel-join-derived-tables-properly.html

    I created a scope query for that thing. I think it can also be in macros..

    public function scopeJoinDerived($query, $derivedQuery, $table, $one, $operator = null, $two = null, $type = 'inner', $where = false)
    {
        $query->join(DB::raw("({$derivedQuery->toSql()}) as `{$table}`"), $one, $operator, $two, $type, $where);
        $join = last($query->getQuery()->joins);
        $join->bindings =  array_merge($derivedQuery->getBindings(), $join->bindings);
    
        return $query;
    }
    
    0 讨论(0)
提交回复
热议问题