Back

I was recently working on a project where I wanted to make a classes functions available inside of the built in Laravel Str:: helper. Laravel Macros are the answer to this issue. However, I wanted to make adding additional methods an easy experience and needing to write and maintain a separate macro with argument definitions for each function was clunky.

foreach (get_class_methods(AdvStr::class) as $methodName) {
Str::macro($methodName, function () use ($methodName) {
$args = func_get_args();

return (new AdvStr())->$methodName(...$args);
});


explanation -> mattstenson.com/dynamically-macro-a-class-to-a-laravel-macroable-class

131

  • No matching results...
  • Searching...

/ 1000