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

afoysal's avatar

PHP code inside Laravel 5 Blade Template

I have to place some PHP code inside Laravel 5 Blade Template. Like below

@foreach ($farmer->tasks as $task) @if ($task->pivot->due_at) < date(now)) $style = 'alert alert-danger'; @elseif ($task->pivot->due_at) > date(now)) $style = 'alert alert-success'; @else $style = ''; @endif @endforeach

Which is the actual procedure to place PHP code inside Laravel 5 Blade Template ??

0 likes
3 replies
bestmomo's avatar

You can use any plain PHP code in Blade template in addition to to Blade syntax. What do you want to achieve ?

afoysal's avatar

I would not like use PHP code, I would like to use Blade syntax. How can I do that in my above code ?? Thanks.

Kryptonit3's avatar

If you need multiple conditions then try this

{{ ($task->pivot->due_at < date(now)) ? 'alert alert-danger' : (($task->pivot->due_at > date(now)) ? 'alert alert-success' : '') }}

But to be honest, you could do it with a single layer ternary statement like this

{{ ($task->pivot->due_at < date(now)) ? 'alert alert-danger' : 'alert alert-success' }}
1 like

Please or to participate in this conversation.