Inside this article we will see the concept i.e How to Create and Use Laravel 9 Macro Example Tutorial. Article contains the classified information about Laravel Macros – Extending Laravel’s Core Classes.
If you are looking for a solution i.e What are Laravel Macros and How to Extend then this article will help you a lot for this. Tutorial is super easy to understand and implement it in your code as well.
Laravel Macros are a great way of extending Laravel’s core classes and add extra functionality required for your application.
Read More: Laravel 9 How to get Random Database Records Tutorial
Let’s get started.
Laravel Installation
Open terminal and run this command to create a laravel project.
composer create-project laravel/laravel myblog
It will create a project folder with name myblog inside your local system.
To start the development server of laravel –
php artisan serve
URL: http://127.0.0.1:8000
Assuming laravel already installed inside your system.
What is Laravel Macro?
In simple word, Laravel Macro is a way to add some missing functionality to Laravel’s internal component with a piece of code which doesn’t exist in the Laravel class.
To implement a Laravel Macro, Laravel provides a PHP trait called Macroable.
We can check for example Response class of Laravel locating at Illuminate\Http\Response
which implements the Macroable
trait, which means you can extend the Response class using a Macro.
Macroable Laravel’s Classes
The following classes of Laravel allow for macros to be created by using the Illuminate\Support\Traits\Macroable trait.
Read More: CodeIgniter 4 Call Spark Command From Controller Tutorial
Here are some of the most commonly used classes to create macros.
- Request: Illuminate\Http\Request
- Response: Illuminate\Http\Response
- Collection: Illuminate\Support\Collection
- Str: Illuminate\Support\Str
- Router: Illuminate\Routing\Router
- UrlGenerator: Illuminate\Routing\UrlGenerator
- Cache: Illuminate\Cache\Repository
- Filesystem: Illuminate\Filesystem\Filesystem
- Arr: Illuminate\Support\Arr
- Rule: Illuminate\Validation\Rule
Create a Laravel Macro
Before creating a Laravel Macro, we have to make sure our targeted class use the Macroable trait. Here we will be create a macro on Illuminate\Support\Str class which will check the length of a given string named isLength
.
Register a Macro
Open AppServiceProvider.php from /app/Providers folder. Register macro using boot() method of it.
<?php namespace App\Providers; use Illuminate\Support\ServiceProvider; use Illuminate\Support\Str; class AppServiceProvider extends ServiceProvider { /** * Register any application services. * * @return void */ public function register() { } /** * Bootstrap any application services. * * @return void */ public function boot() { Str::macro('isLength', function ($str, $length) { return static::length($str) == $length ? 'String Length is ' . $length : 'String length is not ' . $length; }); } }
Create Route
Open web.php file from /routes folder. Add this route into it.
//... use Illuminate\Support\Str; Route::get('route-1', function () { dd(Str::isLength('This is a Laravel Macro', 24)); }); Route::get('route-2', function () { dd(Str::isLength('Welcome To Online Web Tutor', 27)); }); //...
Application Testing
Run this command into project terminal to start development server,
php artisan serve
URL: http://127.0.0.1:8000/route-1
Read More: CodeIgniter 4 Call Spark Command From Closure Routes
Output
"String length is not 24"
URL: http://127.0.0.1:8000/route-2
Output
"String Length is 27"
We hope this article helped you to learn How to Create and Use Laravel 9 Macro Example Tutorial in a very detailed way.
If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.