Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

aurawindsurfing's avatar

anything wrong with this approach?

Hi,

Do you see anything wrong with this approach, please?

@if($variant?->price)
 <div class="mt-3">
  <p class="text-3xl text-gray-900">&euro; {{$variant->price ?? ''}}</p>
 </div>
@endif
0 likes
5 replies
aurawindsurfing's avatar

@sergiu - I'm sorry but I removed your reply but wanted to remove Larry the spam bot ;-)

True but if there is no variant then there is no price then there is only euro sign ;-)

Ok but apart from that would you rather:

{{ $variant?->price }}

or

{{ $variant }}

or

{{! is_null($variant) }}

I kinda like the first one

Sergiu17's avatar
Sergiu17
Best Answer
Level 60

@aurawindsurfing if you keep the if statement, then I'd remove null coalescing operator and that's it.

@if($variant?->price)
 <div class="mt-3">
  <p class="text-3xl text-gray-900">&euro; {{ $variant->price }}</p>
 </div>
@endif

and if you want to keep the HTML structure, then you could do something like this

 <div class="mt-3">
  <p class="text-3xl text-gray-900">{!! $variant?->price ? '&euro;' . $variant->price : 'Default price' !!}</p>
 </div>

I used {!! !!}} syntax because of the '&euro;'

jaseofspades88's avatar

I might add you might wish to cast your price attribute on variant to a money object, which then handles formatting and currency logos. A currency/money package such as https://github.com/akaunting/laravel-money would make your platform especially flexible with prices and currencies and exchange rates.

Seems like a bit of a vague thread, really.

1 like
aurawindsurfing's avatar

Thanks @jaseofspades88 its all about this line really @if($variant?->price) just to my eye is not readable at first but the more I think about it the more I like it ;-)

I do not use any money packages and learned to keep everything cents, proves to be the easiest way to deal with problems for me.

@sergiu17 I'll go with the first on as it is mor readable for me.

1 like

Please or to participate in this conversation.