Laravel how to get query with bindings?

前端 未结 14 1005
小蘑菇
小蘑菇 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: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;
    }
    

提交回复
热议问题