Extending Laravel With Macros

Collection::macro('hello', function($name) {
echo 'Hello ' . $name;
});

Collection::hello('John'); // Outputs "Hello John"
class AppServiceProvider
{
public function boot()
{
Collection::macro('hello', function () {
echo 'Hello ' . $name;
}
}
  • Illuminate\Auth\RequestGuard
  • Illuminate\Auth\SessionGuard
  • Illuminate\Cache\Repository
  • Illuminate\Console\Command
  • Illuminate\Console\Scheduling\Event
  • Illuminate\Console\Scheduling\Schedule
  • Illuminate\Cookie\CookieJar
  • Illuminate\Database\Grammar
  • Illuminate\Database\Eloquent\FactoryBuilder
  • Illuminate\Database\Eloquent\Relations\Relation
  • \Illuminate\Database\Query\Builder
  • \Illuminate\Database\Schema\Blueprint
  • \Illuminate\Events\Dispatcher
  • \Illuminate\Filesystem\Filesystem
  • \Illuminate\Http\Request
  • \Illuminate\Http\Response
  • \Illuminate\Http\Client\Factory
  • \Illuminate\Http\Client\Response
  • \Illuminate\Mail\Mailer
  • \Illuminate\Redis\Connections\Connection
  • \Illuminate\Routing\Redirector
  • \Illuminate\Routing\Route
  • \Illuminate\Routing\Router
  • \Illuminate\Routing\UrlGenerator
  • \Illuminate\Support\Arr
  • \Illuminate\Support\Collection
  • \Illuminate\Support\LazyCollection
  • \Illuminate\Support\Optional
  • \Illuminate\Support\Str
  • \Illuminate\Support\Stringable
  • \Illuminate\Support\Testing\Fakes\NotificationFake
  • \Illuminate\Testing\TestResponse
  • \Illuminate\Translation\Translator
  • \Illuminate\Validation\Rule
  • \Illuminate\View\Factory
  • \Illuminate\View\View
class CarbonMixin
{
// extra helper functions for carbon
}
Carbon::mixin(new CarbonMixin());

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store