DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Data-Driven Enterprise Apps: How Laravel Simplifies Analytics Integration
  • Laravel + Next.js Integration Guide (Real-World Setup, 2025)
  • How Laravel Developers Handle Database Migrations Without Downtime
  • Laravel for Beginners: An Overview of the PHP Framework

Trending

  • The Hidden Cost of AI Tokens: Engineering Patterns for 10x Resource Efficiency
  • Jakarta EE 12: Entering the Data Age of Enterprise Java
  • Engineering Closed-Loop Graph-RAG Systems, Part 2: From Prompts to Rules
  • The Middleware Gap in AI Agent Frameworks
  1. DZone
  2. Coding
  3. Frameworks
  4. How to Use Laravel Macro With Example?

How to Use Laravel Macro With Example?

Laravel Macros gives you more flexibility to extend the public interface of the Laravel core macroable classes.

By 
Razet Jain user avatar
Razet Jain
·
Jan. 23, 21 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
22.9K Views

Join the DZone community and get the full member experience.

Join For Free

Macro is a powerful feature of the Laravel framework. Laravel Macros allow you to add custom functionality to internal Laravel components.

In this post, we will be looking at how we can use Laravel Macros and which classes can be used to define Macros.

If you see the Laravel codebase, you'll find countless references to a trait called Macroable. This trait allows you to extend the public interface of a class in real-time.

Creating a Laravel Macro

Before creating a Laravel Macro, you have to make sure your targeted class use the Macroable trait. Here, we will be creating a Macro on the Illuminate\Support\Str class, which will check the length of a given string named isLength. You can define the macro in your AppServiceProvider class’s boot() method.

PHP
 




x


 
1
public function boot()
2
{
3
        Str::macro('isLength', function ($str, $length) {
4

          
5
            return static::length($str) == $length;
6
        });
7
}


Within the boot method of AppServiceProvider, we are defining a isLength macro on Str class which simply compare the length of first parameter with second one.

Now, you can use this Macro anywhere in your application.

PHP
 




xxxxxxxxxx
1


 
1
use Illuminate\Support\Str;
2
dd(Str::isLength('This is a Laravel Macro', 23)); // true


Laravel’s Macroable Classes

The following list are all of the Laravel classes that currently use the Macroable trait and accept macro calls.

  • 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
Macro (computer science) Laravel

Published at DZone with permission of Razet Jain. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Data-Driven Enterprise Apps: How Laravel Simplifies Analytics Integration
  • Laravel + Next.js Integration Guide (Real-World Setup, 2025)
  • How Laravel Developers Handle Database Migrations Without Downtime
  • Laravel for Beginners: An Overview of the PHP Framework

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook