Eloquent Inspector for Laravel
Published on by Paul Redmond
Laravel Eloquent Inspector is a package by Andrea Marco Sartori to inspect models and collect properties, relationships, and more.
Just tagged the v1 of Eloquent Inspector, a package to collect info from @laravelphp models, like properties and relationships.https://t.co/kUJWUhparY
— Andrea Marco Sartori (@cerbero90) January 18, 2022
It can be used for code analysis or classes auto-generation. Fully documented and tested with @pestphp 💝#Laravel #PHP pic.twitter.com/AmJK1q48qf
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']->class;$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.