Extending Laravel classes

There is a method called macro on a lot of built-in Laravel classes. For example Collection, Str, Arr, Request, Cache, File, and so on.

You can define your own methods on these classes like this:

1Str::macro('lowerSnake', function (string $str) {
2 return Str::lower(Str::snake($str));
3});
4 
5// Will return: "my-string"
6Str::lowerSnake('MyString');

Tip given by @mmartin_joo

Like our articles?

Become a Premium Member for $129/year or $29/month
What else you will get:
  • 22 courses (477 lessons, total 38 h 20 min)
  • 2 long-form tutorials (one new every week)
  • access to project repositories
  • access to private Discord