Nasrul Hazim Bin Mohamad

Laravel: CRUD Breadcrumb


Assalamualaikum & Hi,

Today, I just want to share a little tip on managing your breadcrumb when using Laravel Breadcrumbs.

It is common to have following breadcrumb setup:

Main Menu > Module
Main Menu > Module > Create
Main Menu > Module > Edit
Main Menu > Module > Details

And it is kind of messy if you have lots of module to setup the breadcrumbs.

So, I do some digging on Laravel Breadcrumbs codes and found out the main BreadcrubManager class do have use Macroable trait!.

So the solution for the above use case is to extend the functionality of the breadcrumb using macro! Yeah!

Keep it short, grab the following codes, and call it in your service provider \App\Macros\Routing\Breadcrumb::registerMacros() (my favourite approach in registering macros!)

The usage will be like the following:

Now you can attach a lot more breadcrumbs!


Add the following code snippet at the end of your routes/breadcrumbs.php to include multiple breadcrumb files in routes/breadcrumbs/ directory – of course you need to create the directory first.

Thanks for reading!

Laravel: Blueprint Macro


Next, the thing that I want to share is how I create Blueprint Macro. Similar to my previous posts on Response and Route.

But this time, we going to add more functionality to Blueprint which used in migrations.

Update your user migration file:

Now your code will be much cleaner.

Feel free to fork and contribute to Blueprint Macro.

Laravel: Route Macro


Here I want to show, how to extend existing route – get, post, etc. for your application.

I’m going to create a new route Route::setting('module'), which will create all necessary route setup.

Now register this new macro in your AppServiceProvider, in register() method.

Then, you can start use the new route.

To use this new route, you need a controller.

So run php artisan make:controller Settings/ApplicationSettingController -r

Then you may add in routes/web.php to register a new route setting.

In this case, you just need to add Route::setting('application').

Then run php artisan route:list. You should get something like the following:

Laravel: Response Macro


It’s quite straightforward to extend a class functionality with Macro. This time I show how to extend the Response class in Laravel.

Create a class in app/Macros/Http called Response.php and add the content as following:

Then register the Macro in AppServiceProvider

Now you may test your new responses: