phplaravellaravel-blade

Generate dynamic css classes in laravel-blade


I'm in a classical scenario where i want based on wheter an error has occurred or not to change the css classes. As shown below, where has-error should change accordingly.

<div class="form-group has-error">

The project is in Laravel, where we mainly want to do everything in Blade as of now, we came up with the following solution which works, but does not look pretty or intuitive at all, so the question is, is there a more optimal way of doing this?

<div class="form-group
@if($errors->has('email'))
        has-error
@endif
">

Solution

  • Try the if else condition short hand code:

    <form class="form-group {{ $errors->has('email') ? 'has-error' : '' }}">
    

    This seems neat.

    Even the Laravel 5.2 auth forms are done this way.