🕵️ Inspect Laravel Eloquent models to collect properties, relationships and more.

Overview

🕵️ Eloquent Inspector

Author PHP Version Laravel Version Octane Compatibility Build Status Coverage Status Quality Score Latest Version Software License PSR-12 Total Downloads

Inspect Laravel Eloquent models to collect properties, relationships and more.

Install

Via Composer

composer require cerbero/eloquent-inspector

Usage

To inspect an Eloquent model, we can simply pass its class name to the inspect() method:

use App\Models\User;
use Cerbero\EloquentInspector\Inspector;

$inspector = Inspector::inspect(User::class);

An Inspector singleton is created every time a new model is inspected, this lets us inspect the same model multiple times while running the inspection logic only once.

If we need to free memory or cleanup some inspected model information, we can either flush all model inspections, flush only one model inspection or tell an inspection to forget its data:

// flush information of all inspected models
Inspector::flush();

// flush information of an inspected model
Inspector::flush(User::class);

// forget information of the current inspection
Inspector::inspect(User::class)->forget();

To retrieve the class of the inspected model from an Inspector, we can call getModel():

$model = Inspector::inspect(User::class)->getModel(); // App\Models\User

The method getUseStatements() returns an array with all the use statements of a model, keyed by either the class name or the alias:

use Illuminate\Database\Eloquent\Model;
use Foo\Bar as Baz;

class User extends Model
{
    // ...
}

$useStatements = Inspector::inspect(User::class)->getUseStatements();
/*
[
    'Model' => 'Illuminate\Database\Eloquent\Model',
    'Baz' => 'Foo\Bar',
]
*/

Calling getProperties() performs a scan of the model database table and returns an array of Property instances containing the properties information. The array is keyed by the properties name:

$properties = Inspector::inspect(User::class)->getProperties();
/*
[
    'id' => <Cerbero\EloquentInspector\Dtos\Property>,
    'name' => <Cerbero\EloquentInspector\Dtos\Property>,
    ...
]
*/

$properties['id']->name; // id
$properties['id']->type; // int
$properties['id']->dbType; // integer
$properties['id']->nullable; // false
$properties['id']->default; // null

To inspect the relationships of a model, we can call the method getRelationships(). The result is an array of Relationship instances, keyed by the relationship name, containing all the relationships information:

$relationships = Inspector::inspect(User::class)->getRelationships();
/*
[
    'posts' => <Cerbero\EloquentInspector\Dtos\Relationship>,
    'tags' => <Cerbero\EloquentInspector\Dtos\Relationship>,
    ...
]
*/

$relationships['posts']->name; // posts
$relationships['posts']->type; // hasMany
$relationships['posts']->class; // Illuminate\Database\Eloquent\Relations\HasMany
$relationships['posts']->model; // App\Models\Post
$relationships['posts']->relatesToMany; // true

Change log

Please see CHANGELOG for more information on what has changed recently.

Testing

composer test

Contributing

Please see CONTRIBUTING and CODE_OF_CONDUCT for details.

Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.

You might also like...
Use auto generated UUID slugs to identify and retrieve your Eloquent models.

Laravel Eloquent UUID slug Summary About Features Requirements Installation Examples Compatibility table Alternatives Tests About By default, when get

Record created by, updated by and deleted by on Eloquent models automatically.

quarks/laravel-auditors Record created by, updated by and deleted by (if SoftDeletes added) on Eloquent models automatically. Installation composer re

Observe (and react to) attribute changes made on Eloquent models.
Observe (and react to) attribute changes made on Eloquent models.

Laravel Attribute Observer Requirements PHP: 7.4+ Laravel: 7+ Installation You can install the package via composer: composer require alexstewartja/la

Eloquent Befriended brings social media-like features like following, blocking and filtering content based on following or blocked models.
Eloquent Befriended brings social media-like features like following, blocking and filtering content based on following or blocked models.

Laravel Befriended Eloquent Befriended brings social media-like features like following, blocking and filtering content based on following or blocked

Easy creation of slugs for your Eloquent models in Laravel

Eloquent-Sluggable Easy creation of slugs for your Eloquent models in Laravel. NOTE: These instructions are for the latest version of Laravel. If you

Automatically validating Eloquent models for Laravel

Validating, a validation trait for Laravel Validating is a trait for Laravel Eloquent models which ensures that models meet their validation criteria

Tag support for Laravel Eloquent models - Taggable Trait

Laravel Taggable Trait This package is not meant to handle javascript or html in any way. This package handles database storage and read/writes only.

Preferences for Laravel Eloquent models

Preferences for Laravel Eloquent models Use this library to bind multiple key/value pair preferences to your application's Eloquent models. Preference

Laravel package to search through multiple Eloquent models.

Laravel package to search through multiple Eloquent models. Supports sorting, pagination, scoped queries, eager load relationships and searching through single or multiple columns.

Owner
Andrea Marco Sartori
Andrea Marco Sartori
An Eloquent Way To Filter Laravel Models And Their Relationships

Eloquent Filter An Eloquent way to filter Eloquent Models and their relationships Introduction Lets say we want to return a list of users filtered by

Eric Tucker 1.5k Dec 30, 2022
The missing laravel helper that allows you to inspect your eloquent queries with it's bind parameters

Laravel Query Inspector The missing laravel helper that allows you to ispect your eloquent queries with it's bind parameters Motivations Let's say you

Mouad ZIANI 59 Sep 25, 2022
Laravel Eloquent BelongsToThrough relationships

Introduction This inverse version of HasManyThrough allows BelongsToThrough relationships with unlimited intermediate models. Supports Laravel 5.0+. I

Jonas Staudenmeir 804 Jan 3, 2023
Personal CRM. Remember everything about your friends, family and business relationships.

Personal Relationship Manager Monica is a great open source personal relationship management system. Introduction Purpose Features Who is it for? What

Monica 18.5k Jan 5, 2023
Laravel-model-mapper - Map your model attributes to class properties with ease.

Laravel Model-Property Mapper This package provides functionality to map your model attributes to local class properties with the same names. The pack

Michael Rubel 15 Oct 29, 2022
Control frontend access to properties/methods in Livewire using PHP 8 attributes.

This package adds PHP 8.0 attribute support to Livewire. In specific, the attributes are used for flagging component properties and methods as frontend-accessible.

ARCHTECH 83 Dec 17, 2022
Laravel Ban simplify blocking and banning Eloquent models.

Laravel Ban Introduction Laravel Ban simplify management of Eloquent model's ban. Make any model bannable in a minutes! Use case is not limited to Use

cybercog 879 Dec 30, 2022
cybercog 996 Dec 28, 2022
Laravel Nova Ban simplify blocking and banning Eloquent models.

Laravel Nova Ban Introduction Behind the scenes cybercog/laravel-ban is used. Contents Installation Usage Prepare bannable model Prepare bannable mode

cybercog 39 Sep 29, 2022
Package with small support traits and classes for the Laravel Eloquent models

Contains a set of traits for the eloquent model. In future can contain more set of classes/traits for the eloquent database.

Martin Kluska 3 Feb 10, 2022