PHPackages                             naoray/eloquent-model-analyzer - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. naoray/eloquent-model-analyzer

ActivePakage[Utility &amp; Helpers](/categories/utility)

naoray/eloquent-model-analyzer
==============================

Helpful methods to gain more information about eloquent model classes.

v4.0.0(3y ago)16435.0k↓44.4%5[1 PRs](https://github.com/Naoray/eloquent-model-analyzer/pulls)1MITPHPCI failing

Since Apr 5Pushed 2y ago1 watchersCompare

[ Source](https://github.com/Naoray/eloquent-model-analyzer)[ Packagist](https://packagist.org/packages/naoray/eloquent-model-analyzer)[ GitHub Sponsors](https://github.com/naoray)[ RSS](/packages/naoray-eloquent-model-analyzer/feed)WikiDiscussions master Synced 3d ago

READMEChangelog (10)Dependencies (4)Versions (15)Used By (1)

eloquent-model-analyzer
=======================

[](#eloquent-model-analyzer)

[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE.md)[![Total Downloads](https://camo.githubusercontent.com/aee332543b26f7f0c59a1ede86c968722483757dc56d40ade4a315a5f56156d8/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6e616f7261792f656c6f7175656e742d6d6f64656c2d616e616c797a65722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/naoray/eloquent-model-analyzer)[![Tests](https://github.com/naoray/eloquent-model-analyzer/workflows/Run%20Tests%20-%20Current/badge.svg?branch=master)](https://github.com/naoray/eloquent-model-analyzer/workflows/Run%20Tests%20-%20Current/badge.svg?branch=master)

Analyzing an Eloquent Model for its relations and columns can be overwhelming. This little library aims to make it as simple as possible.

You probably wonder why you would ever need to analyze your Models at runtime?! All scenarios I can think of are related to analyzing the codebase to generate some code bits. Here are some scenarios where this might come in handy:

- automatically create factories for your models as shown in [laravel-prefill-factory](https://github.com/naoray/laravel-factory-prefill) or [factory-generator](https://github.com/laravel-shift/factory-generator)
- it could be use to create something like the `trace` command in [laravel-shift/blueprint](https://github.com/laravel-shift/blueprint)

Install
-------

[](#install)

`composer require naoray/eloquent-model-analyzer`

Usage
-----

[](#usage)

### Getting all relations of a Model

[](#getting-all-relations-of-a-model)

There are three different strategies for getting all relation methods of an Eloquent Model:

- checking the return types of the methods
- extracting the return types from the doc method
- call the method directly and check the instance of what is returned

```
// User.php
class User extends Model
{
    public function parent()
    {
        return $this->belongsTo(self::class);
    }

    public function posts()
    {
        return $this->hasMany(Post::class, 'user_id');
    }
}

// get relations
// type of $columns is \Illuminate\Support\Collection
$relations = Analyzer::relations(User::class);

// get the first relation
$relation = $relations->first();

// all relations implement the Arrayable interface
$relation->toArray();
// [
//     'relatedClass' => User::class,
//     'type' => \Illuminate\Database\Eloquent\Relations\BelongsTo::class,
//     'foreignKey' => 'parent_id',
//     'ownerKey' => 'id',
//     'methodName' => 'parent',
// ]
```

The `RelationMethod` Class forwards all method calls which aren't present on the class directly to the underlying `ReflectionMethod` class.

### Getting all Columns of a Model

[](#getting-all-columns-of-a-model)

```
// CreateUserTable.php
public function up()
{
    Schema::create('users', function (Blueprint $table) {
        $table->id();
        $table->string('name');
        $table->string('email')->unique();
        $table->json('bio')->nullable();
    });
}

// get columns
// type of $columns is \Illuminate\Support\Collection
$columns = Analyzer::columns(User::class);

// get a single column by column name
$column = $columns->get('name');

// all columns implement the Arrayable interface
$column->toArray();
// [
//     'name' => 'name',
//     'type' => \Doctrine\DBAL\Types\StringType::class,
//     'unsigned' => false,
//     'unique' => false,
//     'isForeignKey' => false,
//     'nullable' => false,
//     'autoincrement' => false,
// ]
```

The `Column` class forwards all method calls which aren't present on the class directly to the underlying `DBAL\Schema\Column` Class.

Testing
-------

[](#testing)

Run the tests with:

```
vendor/bin/phpunit
```

Changelog
---------

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

Security
--------

[](#security)

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

License
-------

[](#license)

The MIT License (MIT). Please see [License File](/LICENSE.md) for more information.

###  Health Score

40

—

FairBetter than 86% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity43

Moderate usage in the ecosystem

Community17

Small or concentrated contributor base

Maturity64

Established project with proven stability

 Bus Factor1

Top contributor holds 91% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~95 days

Recently: every ~157 days

Total

12

Last Release

1230d ago

Major Versions

v1.0.2 → v2.0.02020-09-23

v2.1.3 → v3.0.02022-02-08

v3.0.1 → v4.0.02023-02-20

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/10154100?v=4)[Krishan König](/maintainers/Naoray)[@Naoray](https://github.com/Naoray)

---

Top Contributors

[![Naoray](https://avatars.githubusercontent.com/u/10154100?v=4)](https://github.com/Naoray "Naoray (91 commits)")[![laravel-shift](https://avatars.githubusercontent.com/u/15991828?v=4)](https://github.com/laravel-shift "laravel-shift (6 commits)")[![crynobone](https://avatars.githubusercontent.com/u/172966?v=4)](https://github.com/crynobone "crynobone (1 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (1 commits)")[![mortenscheel](https://avatars.githubusercontent.com/u/6514342?v=4)](https://github.com/mortenscheel "mortenscheel (1 commits)")

### Embed Badge

![Health badge](/badges/naoray-eloquent-model-analyzer/health.svg)

```
[![Health](https://phpackages.com/badges/naoray-eloquent-model-analyzer/health.svg)](https://phpackages.com/packages/naoray-eloquent-model-analyzer)
```

###  Alternatives

[psalm/plugin-laravel

Psalm plugin for Laravel

3355.3M346](/packages/psalm-plugin-laravel)[flarum/core

Delightfully simple forum software.

201.4M2.3k](/packages/flarum-core)[api-platform/laravel

API Platform support for Laravel

58171.6k14](/packages/api-platform-laravel)[wearepixel/laravel-cart

A cart implementation for Laravel

1374.8k](/packages/wearepixel-laravel-cart)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
