<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Exa Byte</title>
    <description>The latest articles on DEV Community by Exa Byte (@exabyte).</description>
    <link>https://dev.to/exabyte</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2173396%2F338313e0-596f-4680-886c-ebcdbe15d227.png</url>
      <title>DEV Community: Exa Byte</title>
      <link>https://dev.to/exabyte</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/exabyte"/>
    <language>en</language>
    <item>
      <title>Clean Laravel Macros with PHP Attributes</title>
      <dc:creator>Exa Byte</dc:creator>
      <pubDate>Sun, 15 Mar 2026 09:45:17 +0000</pubDate>
      <link>https://dev.to/exabyte/clean-laravel-macros-with-php-attributes-2k39</link>
      <guid>https://dev.to/exabyte/clean-laravel-macros-with-php-attributes-2k39</guid>
      <description>&lt;p&gt;Laravel macros are powerful, but registering them often feels more manual than it should.&lt;/p&gt;

&lt;p&gt;Usually, the flow looks like this:&lt;br&gt;
• write a helper method,&lt;br&gt;
• wrap it in a closure,&lt;br&gt;
• register it in a service provider,&lt;br&gt;
• repeat for every macroable class.&lt;/p&gt;

&lt;p&gt;That works, but it adds boilerplate and spreads macro logic across multiple places.&lt;/p&gt;

&lt;p&gt;I recently looked at theflowbyte/laravel-macro-attribute, a small package that lets you register Laravel macros with PHP 8 attributes instead. The idea is simple: put a #[Macro(...)] attribute on a static method, register the class once, and the package binds the method as a macro for the target class automatically.&lt;/p&gt;

&lt;p&gt;The problem with traditional macro registration&lt;/p&gt;

&lt;p&gt;Laravel’s macro system is great for extending existing classes like:&lt;br&gt;
• Collection&lt;br&gt;
• Request&lt;br&gt;
• Str&lt;br&gt;
• ResponseFactory&lt;br&gt;
• and other macroable classes&lt;/p&gt;

&lt;p&gt;But in real projects, macro registration tends to become messy:&lt;br&gt;
• macros are declared far from where they are registered,&lt;br&gt;
• service providers grow into glue code,&lt;br&gt;
• related macros are harder to discover,&lt;br&gt;
• renaming methods and keeping registrations in sync becomes annoying.&lt;/p&gt;

&lt;p&gt;The bigger the codebase, the more this friction shows up.&lt;/p&gt;

&lt;p&gt;What this package does&lt;/p&gt;

&lt;p&gt;laravel-macro-attribute lets you define macros directly on static methods using a PHP 8 attribute:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="na"&gt;#[Macro(targetClass: Request::class)]&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;isCrawler&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;Request&lt;/span&gt; &lt;span class="nv"&gt;$request&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="kt"&gt;bool&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nv"&gt;$userAgent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;strtolower&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$request&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nb"&gt;header&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'User-Agent'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;''&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;str_contains&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$userAgent&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'bot'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can find installation instructions and full usage examples in the project repository:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/TheFlowByte/laravel-macro-attribute" rel="noopener noreferrer"&gt;https://github.com/TheFlowByte/laravel-macro-attribute&lt;/a&gt;&lt;/p&gt;

</description>
      <category>laravel</category>
      <category>php</category>
      <category>webdev</category>
      <category>opensource</category>
    </item>
  </channel>
</rss>
