Search code examples
laravellaravel-bladelaravel-facade

Shorthand IF within Laravel HTML Form


I have a form input set up using the HTML facade:

{!! Form::text('name', NULL, ['class' => 'form-control ($errors->has("name") ? " has-error" : "")', 'placeholder' => 'Enter your name'] ) !!}

As you can I have put the $error->has inside the class but it is just printing the if statement as I know it would.

Is there anyway I can do what I am trying but keep on using the Form facade?


Solution

  • Try to put the if statement outside the single quotes:

    {!! Form::text('name', NULL, ['class' => 'form-control'.($errors->has("name") ? " has-error" : "").'', 'placeholder' => 'Enter your name'] ) !!}