Eloquent Inspector for Laravel


January 27th, 2022

Eloquent Inspector for Laravel

Laravel Eloquent Inspector is a package by Andrea Marco Sartori to inspect models and collect properties, relationships, and more.

This package provides helpful information about the use statements of a model, model properties, and model relationships:

// All `use` statements for a model
$useStatements = Inspector::inspect(User::class)->getUseStatements();
'Model' => 'Illuminate\Database\Eloquent\Model',
'Baz' => 'Foo\Bar',
// Model properties
$properties = Inspector::inspect(User::class)->getProperties();
$properties['id']->name; // id
$properties['id']->type; // int
$properties['id']->dbType; // integer
$properties['id']->nullable; // false
$properties['id']->default; // null
$relationships = Inspector::inspect(User::class)->getRelationships();
$relationships['posts']->name; // posts
$relationships['posts']->type; // hasMany
// Illuminate\Database\Eloquent\Relations\HasMany
$relationships['posts']->model; // App\Models\Post
$relationships['posts']->relatesToMany; // true

Model relationships and properties return an array, with the key being the property/relationship name and the value being an object with detailed information about that property/relationship.

You can learn more about this package, get full installation instructions, and view the source code on GitHub.

Filed in:

Paul Redmond

Full stack web developer. Author of Lumen Programming Guide and Docker for PHP Developers.