mercredi 27 mars 2019

Laravel Query Conversion Needed

I need to convert the following mysql query into laravel query builder :

            SELECT id.product_id FROM
            (
                SELECT product_id, count(i.product_id) as t, c.filter_group as b
                FROM
                (
                    SELECT f.filter_id, f.product_id, p.filter_group_id
                    FROM ". DB_PREFIX ."product_filter f
                    left join ". DB_PREFIX ."filter p ON p.filter_id = f.filter_id
                    where p.filter_id IN 
                        (
                        " . implode(',', $implode) . "
                        )
                    GROUP BY product_id , filter_group_id
                ) 
                as i,
                (
                    select count(distinct filter_group_id) as filter_group
                    from ". DB_PREFIX ."filter_description f_b
                    WHERE f_b.filter_id in (" . implode(',', $implode) . ")
                ) 
                as c GROUP BY product_id HAVING t = b
            )
            as id

So Far, I have done :

$query = DB::select('id.product_id')
                        ->fromSub(function ($query) {
                            $query->select('product_id', DB::raw('COUNT(i.product_id) AS t'), 'c.filter_set AS b')
                                    ->fromSub(function ($query) {
                                        $query->select('f.filter_id', 'f.product_id', 'p.filter_set_id')
                                            ->from(config('db.tables.EcomRelationProductFilters').' AS f')
                                            ->leftJoin(config('db.tables.EcomFilters').' AS p', 'p.filter_id', '=', 'f.filter_id')
                                            ->whereIn('p.filter_id', $filter_ids)
                                            ->groupBy('product_id', 'filter_set_id');
                                    }, 'i')
                                    ->fromSub(function ($query) {
                                        $query->select(DB::raw('COUNT(distinct filter_set_id) AS filter_set'))
                                            ->from(config('db.tables.EcomFilterDetails').' AS f_b')
                                            ->whereIn('f_b.filter_id', $filter_ids);
                                    }, 'c')
                                    ->groupBy('product_id')
                                    ->having('t', '=', 'b');
                        }, 'id')->toSql();

But I am having issues with this, It says there are syntax erros in my query.



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2WuYUOC
via IFTTT

Aucun commentaire:

Enregistrer un commentaire