Created
March 28, 2019 20:34
-
-
Save calebporzio/08fd6b48cce29fb0fbe3ef9cddbedc8b to your computer and use it in GitHub Desktop.
A little Blade directive to make working with validation errors a bit nicer.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here's mine: https://gist.github.com/MarceauKa/11970514bee91deaa68162e224894a96