Eloquent Inspector for Laravel

Packages

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:

1 
2// All `use` statements for a model
3$useStatements = Inspector::inspect(User::class)->getUseStatements();
4/*
5[
6 'Model' => 'Illuminate\Database\Eloquent\Model',
7 'Baz' => 'Foo\Bar',
8]
9*/
10 
11 
12// Model properties
13$properties = Inspector::inspect(User::class)->getProperties();
14 
15$properties['id']->name; // id
16$properties['id']->type; // int
17$properties['id']->dbType; // integer
18$properties['id']->nullable; // false
19$properties['id']->default; // null
20 
21 
22$relationships = Inspector::inspect(User::class)->getRelationships();
23 
24$relationships['posts']->name; // posts
25$relationships['posts']->type; // hasMany
26// Illuminate\Database\Eloquent\Relations\HasMany
27$relationships['posts']->class;
28$relationships['posts']->model; // App\Models\Post
29$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.