how to bind parameter in sub query in Laravel

I have a sub-query in join in which I am using where clause, but its giving me the error Column not found: 1054 Unknown column 'products.id' in 'where clause' Below is my query:

Product::leftJoin(
                'categories',
                'products.category_id',
                '=',
                'categories.id'
            )
            ->join('vendor_products', function ($join) {
                $join->on(
                    'products.id',
                    '=',
                    'vendor_products.product_id'
                )
            })
            ->leftJoin('brand_products', function ($join) {
                    $join->on(
                        'products.id',
                        '=',
                        'brand_products.product_id'
                    )->on(
                        'brand_products.brand_id',
                        '=',
                        DB::raw('(
                            SELECT brand_id FROM 
                            (SELECT brand_id, COUNT(brand_id) productCount 
                            FROM brand_products
                            where brand_products.product_id = products.id
                            inner join brand on brand_products.brand_id = brand.id
                            GROUP BY brand_id order by productCount desc, DATE(brand.created_at) asc limit 1) as results)')
                    );
            })
            ->get();

this is the line causing error where brand_products.product_id = products.id. Any help would be highly appreciable

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *