PHP code inside a Laravel 5 blade template

abu abu picture abu abu · Jun 27, 2015 · Viewed 91.1k times · Source

I have to place some PHP code inside a 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

What is the actual procedure to place PHP code inside a Laravel 5 blade template?

Answer

user28864 picture user28864 · Nov 12, 2016

According to documentation, in Laravel 5.2 and newer you can use the following code:

@php
{{-- PHP code here --}}
@endphp

Alternatively, you can extend the Blade templating engine as it's described here.

If neither of the above solutions is suitable, you are stuck with the answers given by Armen and by Gonzalo.