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 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

Product Decisions This Supports

  • Developer Productivity & Velocity

    • Enable rapid prototyping by allowing PMs to define reusable model behaviors without modifying core model classes (avoids merge conflicts, reduces boilerplate).
    • Accelerate feature delivery for internal tools or MVPs where domain-specific logic is needed temporarily or for a niche use case.
  • Flexible Architecture for Experimentation

    • Supports A/B testing or feature flags by dynamically adding methods to models (e.g., User::withFeatureX()) without schema changes.
    • Enables "build vs. buy" trade-offs by allowing PMs to prototype custom model logic before committing to a full feature build.
  • Domain-Specific Extensions

    • Use cases like:
      • E-commerce: Product::applyDiscount() for seasonal promotions.
      • SaaS: User::calculateLifetimeValue() for analytics dashboards.
      • Marketplaces: Order::flagForReview() for moderation workflows.
    • Reduces reliance on monolithic service layers for simple model extensions.
  • Roadmap Alignment

    • Justifies investment in Laravel if the stack isn’t already decided, especially for teams prioritizing rapid iteration.
    • Complements existing packages like Laravel Scout or Cashier by extending model capabilities without tight coupling.

When to Consider This Package

  • Avoid If:

    • Core Model Logic: The behavior is fundamental to the domain (e.g., User::hasRole() should likely be a trait or base model method).
    • Performance-Critical Paths: Dynamic method calls add minor overhead; use traits or interfaces for high-frequency operations.
    • Strict OOP Principles: Teams enforcing SOLID rules may prefer explicit inheritance or composition over runtime method injection.
    • Existing Solutions: Similar functionality can be achieved with Laravel traits, interfaces, or query scopes (evaluate trade-offs).
    • Non-Laravel Stacks: Not applicable for teams using Symfony, Django, or other frameworks.
  • Adopt If:

    • Prototyping or Spikes: Need to test model logic quickly without polluting the codebase.
    • Plugin/Modular Architecture: Building a system where third-party extensions can add model methods (e.g., plugins for a CMS).
    • Legacy Codebases: Safely extend models without modifying existing classes (e.g., vendor or inherited code).
    • Dynamic Workflows: Logic changes frequently (e.g., marketing campaigns, experimental features).

How to Pitch It (Stakeholders)

For Executives:

"This package lets our dev team add custom functionality to Laravel models on the fly—like giving a model a 'temporary superpower' without rewriting code. For example, we could add a Product::applyBlackFridayDiscount() method for a one-time campaign without touching the core product logic. It’s like Lego blocks for model behavior: fast to assemble, easy to disassemble. This reduces dev time for experiments, A/B tests, or niche features, helping us ship faster without over-engineering."

Why it matters:

  • Speed: Cuts weeks off prototyping custom logic (e.g., new user onboarding flows).
  • Flexibility: Supports agile pivots (e.g., adding a feature flag for a pilot program).
  • Cost-Effective: Avoids hiring senior devs to architect complex solutions for one-off needs.

Risk: Minimal—MIT licensed, lightweight, and isolated to model extensions.


For Engineering:

"This is a clean way to add ad-hoc methods to Eloquent models at runtime, using Laravel’s macro system. Think of it as a safer, more maintainable alternative to monkey-patching or dynamic eval().

Use Cases:

  • Dynamic Scopes: Add User::withPremiumFeatures() without cluttering the base model.
  • Feature Flags: Toggle methods like Order::enableTaxExemption() for specific user segments.
  • Third-Party Extensions: Let plugins inject model methods (e.g., a payment gateway adding Invoice::processWithStripe()).

How It Works:

  1. Register macros globally or per-model:
    Model::macro('customMethod', fn($model) => $model->doSomething());
    
  2. Use them like native methods:
    $user->customMethod();
    

Trade-offs:

  • Pros: No base class changes, easy to remove, great for experiments.
  • Cons: Slightly less performant than static methods (but negligible for most cases). Not for production-critical logic.

Alternatives: Compare with traits or query scopes—this wins for temporary or plugin-like extensions."*

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