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
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • AI Is Making PHP Cool Again
  • 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

Trending

  • Claude Code Superpowers: The 5-Tool Stack That Ends Limits and Context Amnesia
  • Managing Technical Debt in AI-Generated Code
  • Java 27 Makes G1 the Default Everywhere: How to Test Your Small Containers Before You Upgrade
  • 5 Mistakes Companies Make When Working With Staff Engineers

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  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
·
Jan. 23, 21 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
23.1K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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

  • AI Is Making PHP Cool Again
  • 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

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • 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