M
M
maaestr02022-03-16 11:12:47
Laravel
maaestr0, 2022-03-16 11:12:47

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>


Is it normal practice to write such large conditions in a template?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
Sergey delphinpro, 2022-03-16
@maaestr0

@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;
}

P
pLavrenov, 2022-03-16
@pLavrenov

No fool proof)
If $course->discount_exists === true but $course->discount->first() is not then the page will crash.

V
Vasily Bannikov, 2022-03-16
@vabka

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 question

Ask a Question

731 491 924 answers to any question