- What is dixipro/magicpro and what problem does it solve in Laravel?
- MagicPro is a lightweight Laravel package that lets you expose computed values on Eloquent models through the familiar $model->property syntax. Instead of writing repetitive accessors, appending attributes manually, and duplicating logic across controllers and resources, you declare a virtual property once and use it anywhere. It is aimed at developers who want more expressive models with less boilerplate.
- How do I install dixipro/magicpro with Composer?
- Install the package with Composer using composer require dixipro/magicpro. The package is described as zero-configuration for modern Laravel applications, so no manual service provider registration or config publishing is required in the standard setup. After installation, you can start declaring magic properties on your Eloquent models.
- Which Laravel and PHP versions does MagicPro support?
- MagicPro targets modern Laravel applications and integrates directly with Eloquent, so it is intended for current Laravel releases rather than legacy 8.x and older stacks. Check the package's composer.json on the dixipro/magicpro repository for the exact Laravel and PHP version constraints before upgrading. Older applications may need a compatibility layer or a newer framework version first.
- How do I define a magic property on an Eloquent model?
- You declare virtual properties using the package's fluent API inside the model, defining the property name and the computation that produces its value. Once declared, the value is available through normal property access such as $model->total or $user->display_name. The definition lives in one place, so you avoid scattering the same logic across accessors and appends.
- Do MagicPro properties show up in toArray() and JSON responses?
- Yes. MagicPro integrates with Eloquent serialization, so declared magic properties are included when a model is converted with toArray() or returned as JSON from a controller. This removes the need to maintain a separate $appends array for every computed value. It also means API responses stay consistent with what you see when accessing properties directly.
- How does lazy evaluation and caching work for computed properties?
- MagicPro evaluates a magic property the first time it is accessed and caches the result for the lifetime of that model instance. This avoids recalculating expensive values when the same property is read multiple times in a request, for example in a Blade view and again in an API resource. Because the cache is per instance, a fresh model instance will recompute the value.
- Are MagicPro magic properties type-safe and null-safe?
- The package supports type-safe casting of computed values along with null-safe access, so a property backed by a nullable relation or optional attribute returns predictable results instead of throwing errors. This makes magic properties safer to use in JSON responses and Blade templates where data may be missing. You should still validate the underlying data your computation depends on.
- Can I use MagicPro in an API-first or headless Laravel application?
- Yes. MagicPro has no frontend dependencies, so it fits headless Laravel backends, mobile APIs, and JSON:API or REST resources built on Eloquent. Because magic properties serialize automatically, they are convenient for exposing derived fields such as totals, statuses, or formatted values to API clients. Keep an eye on how many computed properties a resource exposes, since each one may trigger queries if not eager loaded.
- How do I test MagicPro properties with PHPUnit or Pest?
- Magic properties are ordinary model behavior, so you can test them with standard PHPUnit or Pest feature tests by creating a model instance and asserting on the property value. Use factories to control the underlying attributes and relationships, and assert both the direct property access and the toArray() output. If a computation depends on relationships, assert query counts to confirm you are eager loading correctly.
- When should I avoid MagicPro and use accessors or appends instead?
- Magic properties rely on dynamic property resolution, which can feel opaque for teams following strict domain-driven or CQRS architectures where explicit methods are preferred. Laravel's native accessors, $appends, and API resources remain a good fit for small models or when you want framework-only dependencies. MagicPro is most valuable when many models share similar computed values and you want one consistent, expressive declaration style.