I am using Laravel 5.3. My first Laravel project/learning experience In my blade file I use the following snippet to show the errors below a field after a PUT or POST request.
In this case the database field is called firstName
@if ($errors->has('firstName'))
<span class="help-block">
<strong>{{ $errors->first('firstName') }}</strong>
</span>
@endif
Now since I have lot of fields, I have keep repeatings this block for every field. I looked up the Laravel docs on Blade templates (Extending Blade section) and thought I could do the following in the AppServiceProvider class (AppServiceProvider .php)
public function boot()
{
//
Blade::directive('showIfError', function($fieldName) {
if ($errors->has('$fieldName')) {
echo "<span class='help-block'>
<strong> $errors->first('$fieldName') </strong>
</span>";
}
});
}
and then use
@showIfError('firstName')
But no luck...I get the error 'Undefined variable: errors'
Looks like the Laravel error collection is not accessible in this view file.
Appreciate any help. Thanks.