How to create custom laravel macros

Emmanuel Paul Mnzava.
2 min readSep 23, 2021

--

Hello guys,

Today i will be going over laravel macros (what they are and how to use them in your project).

Laravel Macros allow you to add custom functionality to internal Laravel components.In this article we will look at which laravel classes can be used to define Macros and how to use them.

How to create a Laravel Macro

Before creating a Laravel Macro, you have to make sure your targeted class use the Macroable trait.Basically laravel comes with a number of classes that use this trait see below

  • Illuminate\Auth\RequestGuard
  • Illuminate\Auth\SessionGuard
  • Illuminate\Cache\Repository
  • Illuminate\Console\Command
  • Illuminate\Console\Scheduling\Event
  • Illuminate\Cookie\CookieJar
  • Illuminate\Database\Eloquent\FactoryBuilder
  • Illuminate\Database\Eloquent\Relations\Relation
  • Illuminate\Database\Grammar
  • Illuminate\Database\Query\Builder
  • Illuminate\Database\Schema\Blueprint
  • Illuminate\Filesystem\Filesystem
  • Illuminate\Foundation\Testing\TestResponse
  • Illuminate\Http\JsonResponse
  • Illuminate\Http\RedirectResponse
  • Illuminate\Http\Request
  • Illuminate\Http\Response
  • Illuminate\Http\UploadedFile
  • Illuminate\Mail\Mailer
  • Illuminate\Routing\PendingResourceRegistration
  • Illuminate\Routing\Redirector
  • Illuminate\Routing\ResponseFactory
  • Illuminate\Routing\Route
  • Illuminate\Routing\Router
  • Illuminate\Routing\UrlGenerator
  • Illuminate\Support\Arr
  • Illuminate\Support\Collection
  • Illuminate\Support\LazyCollection
  • Illuminate\Support\Str
  • Illuminate\Support\Testing\Fakes\NotificationFake
  • Illuminate\Translation\Translator
  • Illuminate\Validation\Rule
  • Illuminate\View\Factory
  • Illuminate\View\View

All the above classes use this trait hence one can add more functionalities to them easily so as to simplyfy coding.

Example.

Iam going to create a macro to the class “ Builder ” Illuminate\Database\Query\Builder

Where i will create a query builder search which will help me to search for various items from all models that i have in my Laravel application.

Say i have a Product model which has an attribute of title, In order for me to search for a given title i will normally write something like this

Product::where('title','LIKE','%'.$searchterm.'%')->get()

Note instead of writing the above ,i can achieve the same thing by just rewritting it with a macro called search as seen below

Product::search('title',$searchterm)->get()

So how do you achieve that ?

To create such macro i will first navigate to App\Providers\AppServiceProvider.php

Then head over the boot method inside it add the macro as seen below

<?phpnamespace App\Providers;use Illuminate\Support\Facades\Blade;use Illuminate\Support\Facades\Schema;use Illuminate\Database\Eloquent\Builder;use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{/*** Register any application services.** @return void*/public function register(){//}/*** Bootstrap any application services.** @return void*/public function boot(){Builder::macro('search', function ($column, $searchstring) {return $string ? $this->where($column, 'like', '%' . $searchstring . '%') : $this;});
}

As seen above we have used the class builder to add a macro called “search” that will handle a search query by accepting column and a search string.

The above is just an example of away to add macro but possibilities are endless.

Test the above and let me know how it goes on the comment section

And hey I would like to introduce to you storewid.com, an eCommerce platform that can help you build, manage and grow your online store with ease.
Visit storewid.com and try it out for free.

Before you go… Thanks for reading the article! If you enjoyed it, please don’t forget to show your appreciation by clicking 👏 below!

Any questions or comments hit me up on

Mail: epmnzava@gmail.com

Twitter: https://twitter.com/epmnzava

Github: https://github.com/dbrax

--

--

Emmanuel Paul Mnzava.

Software Engineer and techprenuer with passion of helping entreprenuers and small businesses using Technology.