Answer the question
In order to leave comments, you need to log in
Is this practice normal?
Hello. I have a product and a discount on it. This discount must be displayed on the product card. I do like this:
<div class="courses-body__prices">
@if (!$course->discount_exists)
<span class="courses-body__price">
{{$course->price->price}}$
</span>
@else
<s class="courses-body__price_crossed">
{{$course->price->price}}$
</s>
<span class="courses-body__price">
{{$course->price->price - ($course->price->price / 100 * $course->discount->first()->size_discount)}}$
</span>
@endif
</div>
Answer the question
In order to leave comments, you need to log in
@if ($course->discount_exists)
<s class="courses-body__price_crossed">
{{$course->price->price}}$
</s>
@endif
<span class="courses-body__price">
{{$course->price->price_with_discount}}$
</span>
public function getPriceWithDisacountAttribute(){
return $this->price - $this->discount;
}
No fool proof)
If $course->discount_exists === true but $course->discount->first() is not then the page will crash.
My opinion: everything is OK, but it is better to put this into a function / method:
$course->price->price - ($course->price->price / 100 * $course->discount->first()->size_discount)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question