Installation:
composer require cerbero/eloquent-inspector
Add the service provider to config/app.php (automatically registered if using Laravel ≥5.5).
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.
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.).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' />";
}
Model Metadata Extraction:
$inspector = Inspector::inspect(MyModel::class);
$fillable = $inspector->getProperties()['fillable'];
$relations = $inspector->getRelationships();
Relationship Mapping:
$relations = Inspector::inspect(Order::class)->getRelationships();
foreach ($relations as $relation) {
if ($relation['type'] === 'hasMany') {
echo "Order #{$relation['name']}: {$relation['related']}";
}
}
Event Hooks Discovery:
$events = Inspector::inspect(User::class)->getEvents();
if (in_array('deleting', $events)) {
// Add custom logic before deletion
}
$cacheKey = 'model_inspection_' . md5(User::class);
$inspector = Cache::remember($cacheKey, now()->addHours(1), fn() =>
Inspector::inspect(User::class)
);
getProperties() to dynamically index searchable fields.toArray() dynamically based on inspected properties.Circular Dependencies:
User->posts->comments->user) may cause infinite loops.Inspector::inspect()->setMaxDepth(2) to limit recursion.Cached Inspections:
Inspector::flush(App\Models\User::class); // After migrations
boot() in models to auto-flush:
protected static function boot() {
parent::boot();
Inspector::flush(static::class);
}
Performance:
php artisan model:inspect).dd(Inspector::inspect(User::class)->toArray());
app/Models/ is in composer.autoload.psr-4.Inspector::inspect()->getErrors() to catch parsing issues.Inspector::extend(MyModel::class, function ($inspector) {
$inspector->addProperty('custom_field', 'value');
});
$inspector->getProperties()['usesSoftDeletes'] = in_array('deleted_at', $model->getDates());
How can I help you explore Laravel packages today?