select('product_has_relations.product_id') ->whereIn('product_has_relations.productable_id', (function ($query) use ($product) { $query->from('product_has_relations') ->select('productable_id') ->distinct('productable_id') ->where('productable_type', '=', 'category') ->where('product_id', '=', $product->id); })) ->limit(12) ->orderByRaw('RANDOM()') ->pluck('product_has_relations.product_id') ->unique(); return response()->rest( ProductResource::collection( ProductRepository::make(request()) ->applyBasicQueries() ->whereIntegerInRaw('id', $products->toArray()) ->get() ) ); } }