Skip to content

Instantly share code, notes, and snippets.

@shibbirweb
Created December 21, 2022 14:04
Show Gist options
  • Save shibbirweb/fc4c3e54f98b41593bcb31e86591804a to your computer and use it in GitHub Desktop.
Save shibbirweb/fc4c3e54f98b41593bcb31e86591804a to your computer and use it in GitHub Desktop.
Laravel helpful custom directive
<?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