Files
backend-mm/app/Http/Controllers/Api/V1/Product/ProductRelatedController.php
2025-09-25 03:03:31 +05:00

43 lines
1.4 KiB
PHP

<?php
namespace App\Http\Controllers\Api\V1\Product;
use App\Http\Controllers\Api\V1\Product\Resources\ProductResource;
use App\Http\Controllers\Controller;
use App\Models\Ecommerce\Product\Product\Product;
use App\Repositories\Ecommerce\Product\ProductRepository;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\DB;
class ProductRelatedController extends Controller
{
/**
* Related products (index)
*/
public function index(Product $product): JsonResponse
{
$products = DB::table('product_has_relations')
->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()
)
);
}
}