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

brakkar's avatar

Ternary not working in blade field curly braces

Hi, in a reusable form, I try to handle 3 conditions for a field: old value, edit value, and empty:

<input type="text" value="{{old('subject', $topic ? $topic->subject : ' '  )}}"/>

I was thinking this would work, but it gives a "Undefined variable $topic" error.

How to handle this situation?

0 likes
5 replies
Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

Check if the $topic variable exists (Not sure why the variable itself would not be set though)

<input type="text" value="{{old('subject', isset($topic) ? $topic->subject : ' '  )}}"/>
1 like
Sinnbeck's avatar

@brakkar the ?-> version only works if $topic is set at all (if it's set to null)

1 like

Please or to participate in this conversation.