perf(bo): optimize product selection and association queries in BO controllers [AUD-PER-012]

This commit is contained in:
Jérémy Munsch 2026-08-26 11:11:50 +02:00
parent 29fa8a1b7d
commit 668aaba1ca
No known key found for this signature in database
GPG key ID: 2AAECDA6B7330F91
2 changed files with 25 additions and 10 deletions

View file

@ -41,7 +41,10 @@ class DiscountsController extends Controller
$this->authorize('create', Discount::class);
$discountTypes = DiscountType::cases();
$existingProds = Product::all();
$existingProds = Product::query()
->select(['id', 'name'])
->orderBy('name')
->get();
$selectedProds = [];
return view('bo.discounts.create', compact(
@ -87,9 +90,14 @@ class DiscountsController extends Controller
$discountTypes = DiscountType::cases();
$existingProds = Product::query()->get();
$selectedProds = $discount->products;
$existingProds = $existingProds->diff($selectedProds);
$selectedProds = $discount->products()
->select(['products.id', 'products.name'])
->get();
$existingProds = Product::query()
->select(['id', 'name'])
->whereNotIn('id', $selectedProds->pluck('id'))
->orderBy('name')
->get();
return view('bo.discounts.edit', compact(
'discount',

View file

@ -160,7 +160,10 @@ class ProductsController extends Controller implements MassActions
$ranges = Range::query()->pluck('name', 'id');
$tags = Tag::query()->select('label', 'id', 'slug')->get();
$existingProds = Product::all();
$existingProds = Product::query()
->select(['id', 'name'])
->orderBy('name')
->get();
$selectedProds = [];
return view('bo.products.create', compact(
@ -215,7 +218,7 @@ class ProductsController extends Controller implements MassActions
{
$this->authorize('update', $product);
$product->load(['tags', 'products', 'inco']);
$product->load(['tags', 'inco']);
$brands = ProductBrand::cases();
$taxes = TaxRate::cases();
@ -225,11 +228,15 @@ class ProductsController extends Controller implements MassActions
$ranges = Range::query()->pluck('name', 'id');
$tags = Tag::query()->select('label', 'id', 'slug')->get();
$existingProds = Product::query()
->where('id', '!=', $product->id)
$selectedProds = $product->products()
->select(['products.id', 'products.name'])
->get();
$existingProds = Product::query()
->select(['id', 'name'])
->where('id', '!=', $product->id)
->whereNotIn('id', $selectedProds->pluck('id'))
->orderBy('name')
->get();
$selectedProds = $product->products;
$existingProds = $existingProds->diff($selectedProds);
return view('bo.products.edit', compact(
'product',