How can I make sub query in laravel eloquent?
When I use db raw, it works My query is using db raw like this : $products = DB::select(DB::raw('SELECT * FROM ( SELECT a.*, b.name AS store_name, b.address FROM products a JOIN stores b ON b.id = a.store_id WHERE a.category_id = '.$category_id.' ORDER BY a.total_sold DESC, a.updated_at DESC LIMIT '.$num.' ) AS product GROUP BY store_id')); It works. But I want to change it use laravel eloquent I try like this : $products = Product::where('category_id', '=', $category_id) ->with('store') ->groupBy('store_id') ->orderBy('total_sold','desc') ->orderBy('updated_at', 'desc') ->take($num) ->get();