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

Eloquent Mutators Laravel Package

adrolli/eloquent-mutators

Define reusable Eloquent accessors and mutators outside your models. Apply the same transformation logic across multiple models or multiple attributes on one model using a base model class or a trait, with config and extensible registration via a service provider.

View on GitHub
Deep Wiki
Context7

Product Decisions This Supports

  • Code Reusability & DRY Principles: Enables centralized definition of accessors/mutators (e.g., text normalization, case conversion, slug generation) to avoid duplication across models. Reduces technical debt by consolidating logic in a single package.
  • Consistent Data Transformation: Standardizes data formatting (e.g., title_case, slug, trim_whitespace) across all models, improving UX and API consistency.
  • Scalability for Multi-Model Projects: Ideal for large applications with shared business logic (e.g., e-commerce, CMS) where attributes like name, description, or price require uniform transformations.
  • Build vs. Buy Decision: Justifies buying this package over custom development for teams lacking time/resources to build reusable mutators from scratch. Lowers maintenance overhead compared to in-house solutions.
  • Roadmap for Feature Expansion: Supports future needs like:
    • Dynamic Mutators: Extending with custom logic (e.g., conditional formatting based on user roles).
    • Performance Optimization: Caching frequently used mutators (e.g., slug generation).
    • Localization: Adding language-specific mutators (e.g., pluralization for non-English languages).

When to Consider This Package

  • Adopt When:

    • Your Laravel app has 3+ models requiring similar accessors/mutators (e.g., trim_whitespace, slug).
    • You need to reuse logic across attributes/models without duplicating code.
    • Your team prioritizes consistency in data formatting (e.g., API responses, UI displays).
    • You’re using Laravel 5.4+ and want minimal setup overhead.
  • Look Elsewhere If:

    • Your mutators require highly complex, model-specific logic (e.g., business rules tied to domain objects). Custom mutators in the model class may be clearer.
    • You’re using non-Eloquent models (e.g., plain PHP objects) or frameworks outside Laravel.
    • Your team prefers zero dependencies or has strict security policies against third-party packages.
    • You need real-time mutators (e.g., database triggers) where Eloquent accessors/mutators are insufficient.

How to Pitch It (Stakeholders)

For Executives:

"This package lets us standardize how data is formatted across our application—like automatically cleaning up text fields or generating SEO-friendly URLs—without writing repetitive code. It’s a low-risk, high-reward way to improve data quality and developer efficiency, especially as we scale. Think of it as a ‘copy-paste eliminator’ for our backend team."

Key Outcomes:

  • Faster Development: Reduces boilerplate code for common transformations.
  • Consistency: Ensures all user-generated content (e.g., titles, descriptions) follows the same rules.
  • Maintainability: Changes to formatting logic (e.g., switching from snake_case to kebab_case) require updates in one place.

For Engineers:

*"This package moves Eloquent accessors/mutators out of individual model files into a reusable, configurable system. Here’s how it helps you:

  • Reuse Logic: Define a slug mutator once and apply it to Post, Product, and Category models.
  • Extensible: Add custom mutators (e.g., obfuscate_email) via the Mutator facade without touching model classes.
  • Cleaner Models: Keeps model files focused on business logic, not formatting.
  • Built-in Tools: Includes 15+ mutators for text manipulation (case conversion, whitespace, slugs, etc.).

Implementation:

  1. Install via Composer (composer require awobaz/eloquent-mutators).
  2. Choose between extending the base Model class or using the Mutable trait.
  3. Configure mutators in your models (e.g., $accessors = ['title' => 'trim_whitespace']).
  4. Extend with custom logic via the Mutator facade.

Trade-offs:

  • Adds a minor dependency (MIT-licensed, actively maintained).
  • Requires initial setup (publish config, update models).
  • Not ideal for mutators with heavy model-specific dependencies.

Example Use Case: Instead of this in every model:

public function getTitleAttribute($value) {
    return trim(strtolower($value));
}

Use this once in a config file and apply it everywhere:

protected $accessors = [
    'title' => ['trim_whitespace', 'lower_case']
];
```"*
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.
besmartand-pro/php-quality-config
sentix/ai-chatbot
terminal42/code-quality-tools
codifyo/ts-generator-bundle
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