Closed dijkr closed 1 year ago
For now, every view will load, even when no data is available. The only view that won't load is home. It tries to inject 3 random promotion-products. There is no data in the database yet. The view should be changed, so it won't handle the code when there isn't any data available.
You requested 3 items, but there are only 0 items available.
Solution. With the new code, the view will be returned without data. If there is data, the view will return with data:
public function showRandomPromotions (Request $request)
{
// Get three random promotion-products
$promotions = Promotion::with('product')
->where('ValidUntil', '<=', '26-06-2023')
->get();
// Return the view without promotions, if $promotions is null
if($promotions->isEmpty()) {
return View::make('home')
->layout('layout');
}
$randomPromotions = $promotions->random(3);
return View::make('home')
->layout('layout')
->with(['promotions' => $randomPromotions ]);
}
};
Some pages (most of them) use data from the database. The view only need to try to put data in there, if there actualy is data. This will help a smooth deployment and also is a great desgin aspect to solve in general, in my opinion.