How to use Blade directives conditionally in Laravel

Categorized as Laravel Tagged

If you need to apply some css classes conditionally in your Laravel Blade templates, you can do it with the following technique:

<table>
@foreach ($items as $item)
  <tr @class(['bg-white' => $loop->odd, 'bg-gray-50' => $loop->even])>
    ...
  </tr>
@endforeach
</table>

In this example we’ve created a table with different background for odd and even rows using Tailwind CSS classes.

Leave a reply

Your email address will not be published.