Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A little Blade directive to make working with validation errors a bit nicer.
<?php
// Usage:
// Before
@if ($errors->has('email'))
<span>{{ $errors->first('email') }}</span>
@endif
// After:
@error('email')
<span>{{ $message }}</span>
@enderror
// Include the following in your "app/Providers/AppServiceProvider.php":
class AppServiceProvider extends ServiceProvider
{
public function boot()
{
Blade::directive('error', function ($expression) {
return <<<EOT
<?php
if (\$errors->has({$expression})) :
if (isset(\$message)) { \$messageCache = \$message; }
\$message = \$errors->first({$expression});
?>
EOT;
});
Blade::directive('enderror', function () {
return <<<EOT
<?php
unset(\$message);
if (isset(\$messageCache)) { \$message = \$messageCache; }
endif;
?>
EOT;
});
}
@MarceauKa

This comment has been minimized.

@jartaud

This comment has been minimized.

Copy link

@jartaud jartaud commented Mar 30, 2019

@MarceauKa yours is for BS only.

@27Saidou

This comment has been minimized.

Copy link

@27Saidou 27Saidou commented Mar 30, 2019

it's very #interesting

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment