fix: add eager loading for product images in BrandController

Fixes lazy loading violation on Product.images relation which was
causing BrandDashboardTest failures. Added `with('images')` eager
loading in both the dashboard method and calculateBrandInsights.
This commit is contained in:
kelly
2025-12-09 09:50:43 -07:00
parent bb60a772f9
commit a049db38a9

View File

@@ -145,8 +145,8 @@ class BrandController extends Controller
{
$this->authorize('view', [$brand, $business]);
// Load relationships
$brand->load(['business', 'products']);
// Load relationships (include images to avoid lazy loading in view)
$brand->load(['business', 'products.images']);
// Get stats data for Analytics tab (default to this month)
$preset = $request->input('preset', 'this_month');
@@ -1721,7 +1721,8 @@ class BrandController extends Controller
*/
private function calculateBrandInsights(Brand $brand, Business $business, $startDate, $endDate): array
{
$products = $brand->products;
// Eager load images to avoid N+1 and lazy loading errors
$products = $brand->products()->with('images')->get();
// Top Performer - product with highest revenue in date range
$topPerformer = null;