Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message
Laravel Macroable Models

Laravel Macroable Models Laravel Package

javoscript/laravel-macroable-models

Adds Macroable support to Eloquent models, letting you define and register reusable model macros for dynamic methods at runtime. Great for extending models cleanly across packages and projects without touching the model class directly.

View on GitHub
Deep Wiki
Context7

A package for adding methods to Laravel models on the fly 🕊

Frequently asked questions about Laravel Macroable Models
How do I add a custom method to a Laravel model without editing the model file directly?
Use macros to dynamically inject methods. For example, add `isAdmin()` to the `User` model with `User::macro('isAdmin', function() { return $this->role === 'admin'; });`. This avoids cluttering the model class while keeping logic reusable.
Will macros work with Laravel 10 or only older versions?
This package is designed for Laravel 8+, including Laravel 10. However, always verify compatibility if using newer Laravel releases, as macro syntax or Eloquent behavior might evolve. Test thoroughly in your environment.
Can I use macros to replace traits or accessors in my Laravel app?
Yes, macros are a great alternative to traits or accessors when you need dynamic, runtime-added methods. For example, replace repetitive logic like `hasActiveSubscription()` across controllers with a single macro definition on the model.
How do I test macros in PHPUnit? Macros aren’t static methods.
Use PHPUnit’s `getMockBuilder()` to mock the macro method. For example, `$user = $this->getMockBuilder(User::class)->setMethods(['isAdmin'])->getMock();` then assert behavior. Alternatively, create test helpers to register macros temporarily during tests.
Are macros slower than regular model methods in production?
Macros introduce negligible runtime overhead since they’re resolved dynamically. For most applications, the performance impact is insignificant. If you’re working with high-throughput systems, benchmark macros against alternatives like traits or accessors.
Can I use this package to extend models in third-party Laravel packages?
Absolutely. Macros are ideal for package development because they allow you to add reusable methods to models without requiring users to modify their own code. Document the macros clearly in your package’s README for easy adoption.
How do I ensure macros don’t conflict with existing model methods?
Macros won’t override existing methods unless you explicitly define a macro with the same name. Always check for naming conflicts, especially when working with core Laravel models or third-party packages that extend Eloquent.
Do macros support dependency injection or closures with parameters?
Macros accept closures, so you can inject dependencies or parameters dynamically. For example, `User::macro('sendNotification', function($type) { ... })` allows passing arguments when calling the macro method.
What’s the best way to document macros for my team?
Create a `MACROS.md` file in your project or package repo detailing each macro’s purpose, parameters, and usage examples. Include return types and side effects to help developers use them correctly. Link to this file in your main README.
Are there any security risks with dynamically added model methods?
Macros themselves don’t introduce security risks, but ensure the logic inside them follows Laravel’s security best practices. For example, validate inputs in macro closures just as you would in regular model methods or controllers.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
terminal42/code-quality-tools
codifyo/ts-generator-bundle
andydefer/laravel-cluster
testo/fiber
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity
christhompsontldr/laravel-inky