DEV Community

AriaieBOY
AriaieBOY

Posted on • Originally published at ariaieboy.ir

What is a Laravel Macro, and how can you write an IDE Helper for a Macro?

What is a Macro in Laravel?

Macro is a simple mechanism that lets you add extra functionality to the classes that use the Macroable trait.

For example, in my filament-jalali-datetime plugin for filament, I added a jalaliDate method to the filament TextColumn class using the code below:

TextColumn::macro('jalaliDate', function (?string $format = null, ?string $timezone = null): static {
            $format ??= config('filament-jalali-datetime.date_format');

            $this->formatStateUsing(static function (Column $column, $state) use ($format, $timezone): ?string {
                /** @var TextColumn $column */

                if (blank($state)) {
                    return null;
                }

                return Jalalian::fromCarbon(Carbon::parse($state)
                    ->setTimezone($timezone ?? $column->getTimezone()))
                    ->format($format);
            });

            return $this;
        });

Enter fullscreen mode Exit fullscreen mode

You can add new functionality to any class that uses the Illuminate\Support\Traits\Macroable trait.

It's valuable, especially when you want to add new functionality to the core components of the framework and share it as a package.

The IDE-Helper

Laravel Macros are magical, and your IDE might not detect them correctly, so to have better IDE support, you must create some IDE helpers so the IDE can see your macros.

The simplest way I found is an ide_helpers.stubs.php on the root that contains the signature of your macros; for example, for my jalaliDate method, I created this file, and my IDE can detect this method without any problem:

<?php

namespace Filament\Tables\Columns {
    class TextColumn
    {
        /**
         * @source packages/filament-jalali-datetime/src/FilamentJalaliDatetimeServiceProvider.php:28
         */
        public function jalaliDate(?string $format = null, ?string $timezone = null): self
        {
            return $this;
        }
}
Enter fullscreen mode Exit fullscreen mode

There is also a package to create automatic IDE-Helpers for macros named tutorigo/laravel-ide-macros.

If you have any other option to create IDE-Helpers for Laravel macros, you can share it in the comments.atabases, Object Storage, and git repositories.
I need to check the integrity of the backup files.

Billboard image

Use Playwright to test. Use Playwright to monitor.

Join Vercel, CrowdStrike, and thousands of other teams that run end-to-end monitors on Checkly's programmable monitoring platform.

Get started now!

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay