AngularAngularAngular AngularAngularAngular - 1 year ago 50
PHP Question

How to add class to the error messages in Laravel automaticallly

In the Blade i use

{{ $errors->first('VehicleNumber') }} to throw the errors.

But How can i add the div class to it.

I tried

{{ $errors->first('VehicleNumber', array('class' => 'error')) }}

But It throws Array to String Conversion Error.

I don't need

<div class="error">
{{ $errors->first('VehicleNumber') }}

If i do so the class error will always appear.

But I want to appear the class only if the error exist

Answer Source

You use the 2nd function parameter to format the message:

{{ $errors->first('VehicleNumber', '<div class="error">:message</div>') }}