Created
December 21, 2022 14:04
-
-
Save shibbirweb/fc4c3e54f98b41593bcb31e86591804a to your computer and use it in GitHub Desktop.
Laravel helpful custom directive
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 | |
namespace App\Providers; | |
use Illuminate\Support\Facades\Blade; | |
use Illuminate\Support\ServiceProvider; | |
class DirectiveServiceProvider extends ServiceProvider | |
{ | |
/** | |
* Register services. | |
* | |
* @return void | |
*/ | |
public function register() | |
{ | |
// | |
} | |
/** | |
* Bootstrap services. | |
* | |
* @return void | |
*/ | |
public function boot() | |
{ | |
/* | |
* component props validation directive | |
* Example: | |
@propsValidation([ | |
empty($name) => 'Name is required', | |
!is_bool($isActive) => 'Is active should be a boolean.', | |
]) | |
*/ | |
Blade::directive('propsValidation', function ($expression) { | |
return ' | |
<?php | |
foreach( ' . $expression . ' as $isTrue => $message){ | |
if($isTrue){ | |
throw new Exception($message); | |
} | |
}; | |
?> | |
'; | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment