I have a discount block on my site. In the table with items there is a column discount
and if it has a value > 0
, then the block with discounts is displayed.
If there are no discounts, the discount block should not be displayed. And there is a problem with this. If there are no discounts, I get an error Undefined offset: 0
.
My code:
$date = Carbon::today();
$count_discount = count(Item::where('discount', ">=", 1)->where('updated_at', '>=', $date)->get());
$item_bd = Item::where('discount', ">=", 1)->where('updated_at', '>=', $date)->orderByRaw('RAND()')->take(5)->get();
$classid = $item_bd[0]->classid;
$name = $item_bd[0]->market_hash_name;
$old_price = $item_bd[0]->price;
$discount = $item_bd[0]->discount;
$new_price = $item_bd[0]->price - (number_format($item_bd[0]->price / 100 * $discount, 2));
And in the blade i use:
@if ($count_discount === 0)
// no discount
@else
// block with dicsount
@endif
If there is a discount in the database, the discount block is displayed perfectly, if there are no discounts, I get an error.
How can I fix this error?