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 Inspector Laravel Package

cerbero/eloquent-inspector

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Steps

  1. Installation:

    composer require cerbero/eloquent-inspector
    

    Add the service provider to config/app.php (automatically registered if using Laravel ≥5.5).

  2. First Inspection:

    use App\Models\User;
    use Cerbero\EloquentInspector\Inspector;
    
    $inspector = Inspector::inspect(User::class);
    

    This returns an Inspector instance with metadata about the model.

  3. Key Outputs:

    • $inspector->getProperties() → Array of model attributes (fillable, guarded, etc.).
    • $inspector->getRelationships() → Array of defined relationships (belongsTo, hasMany, etc.).
    • $inspector->getEvents() → Array of model events (creating, saved, etc.).

First Use Case

Dynamic Form Generation: Use getProperties() to auto-generate form fields for a model without hardcoding:

$properties = Inspector::inspect(User::class)->getProperties();
foreach ($properties as $property) {
    echo "<input name='{$property['name']}' type='text' />";
}

Implementation Patterns

Core Workflows

  1. Model Metadata Extraction:

    $inspector = Inspector::inspect(MyModel::class);
    $fillable = $inspector->getProperties()['fillable'];
    $relations = $inspector->getRelationships();
    
    • Useful for admin panels, API documentation, or dynamic CRUD.
  2. Relationship Mapping:

    $relations = Inspector::inspect(Order::class)->getRelationships();
    foreach ($relations as $relation) {
        if ($relation['type'] === 'hasMany') {
            echo "Order #{$relation['name']}: {$relation['related']}";
        }
    }
    
    • Build graph-based data structures (e.g., nested resource APIs).
  3. Event Hooks Discovery:

    $events = Inspector::inspect(User::class)->getEvents();
    if (in_array('deleting', $events)) {
        // Add custom logic before deletion
    }
    

Integration Tips

  • Caching: Cache inspection results for performance:
    $cacheKey = 'model_inspection_' . md5(User::class);
    $inspector = Cache::remember($cacheKey, now()->addHours(1), fn() =>
        Inspector::inspect(User::class)
    );
    
  • Laravel Scout: Use getProperties() to dynamically index searchable fields.
  • API Resources: Generate toArray() dynamically based on inspected properties.

Gotchas and Tips

Pitfalls

  1. Circular Dependencies:

    • Inspecting models with recursive relationships (e.g., User->posts->comments->user) may cause infinite loops.
    • Fix: Use Inspector::inspect()->setMaxDepth(2) to limit recursion.
  2. Cached Inspections:

    • Flush inspections when models change:
      Inspector::flush(App\Models\User::class); // After migrations
      
    • Tip: Override boot() in models to auto-flush:
      protected static function boot() {
          parent::boot();
          Inspector::flush(static::class);
      }
      
  3. Performance:

    • Inspecting large models (e.g., with 50+ relationships) can be slow.
    • Optimization: Pre-inspect models during command scheduling (e.g., php artisan model:inspect).

Debugging

  • Verify Inspection:
    dd(Inspector::inspect(User::class)->toArray());
    
  • Check for Missing Data:
    • Ensure app/Models/ is in composer.autoload.psr-4.
    • Use Inspector::inspect()->getErrors() to catch parsing issues.

Extension Points

  1. Custom Inspectors: Override the default inspector for specific models:
    Inspector::extend(MyModel::class, function ($inspector) {
        $inspector->addProperty('custom_field', 'value');
    });
    
  2. Add Metadata: Extend the inspector to include soft deletes, timestamps, or casts:
    $inspector->getProperties()['usesSoftDeletes'] = in_array('deleted_at', $model->getDates());
    
  3. Laravel Octane: Inspections are not compatible with Octane’s serverless mode. Use in traditional HTTP requests only.
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.
elnasnato/laraliveui
labrodev/rest-sdk
sampaui/sampaui
babelqueue/php-sdk
facebook/capi-param-builder-php
babelqueue/symfony
hamzi/corewatch
minionfactory/raw-hydrator
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle
atriumphp/atrium