PHPackages                             spatie/laravel-model-info - 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. [Database &amp; ORM](/categories/database)
4. /
5. spatie/laravel-model-info

ActiveLibrary[Database &amp; ORM](/categories/database)

spatie/laravel-model-info
=========================

Get information about the models in your Laravel app

2.0.5(2mo ago)182322.8k↓14.6%2411MITPHPPHP ^8.2CI passing

Since Aug 9Pushed 2mo ago1 watchersCompare

[ Source](https://github.com/spatie/laravel-model-info)[ Packagist](https://packagist.org/packages/spatie/laravel-model-info)[ Docs](https://github.com/spatie/laravel-model-info)[ RSS](/packages/spatie-laravel-model-info/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (10)Dependencies (18)Versions (25)Used By (11)

Get information about the models in your Laravel app
====================================================

[](#get-information-about-the-models-in-your-laravel-app)

[![Latest Version on Packagist](https://camo.githubusercontent.com/6fc15af7153b581fb2cfc4fffb9f02e4fc8c1358f1b7336c6a7f02a96b7c357b/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f7370617469652f6c61726176656c2d6d6f64656c2d696e666f2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/spatie/laravel-model-info)[![Total Downloads](https://camo.githubusercontent.com/4bb7dc9f96da84d0b8a5b59c922b71f07f6dd1e5bc6c9f517ab8651b14cda450/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f7370617469652f6c61726176656c2d6d6f64656c2d696e666f2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/spatie/laravel-model-info)

Using this package you can determine which attributes and relations your model classes have.

```
use Spatie\ModelInfo\ModelInfo;

$modelInfo = ModelInfo::forModel(YourModel::class);

$modelInfo->fileName; // returns the filename that contains your model
$modelInfo->tableName; // returns the name of the table your models are stored in
$modelInfo->attributes; // returns a collection of `Attribute` objects
$modelInfo->relations; // returns a collection of `Relation` objects
```

Here's how you can get information about the attributes:

```
$modelInfo->attributes->first()->name; // returns the name of the first attribute
$modelInfo->attributes->first()->type; // returns the type of the first attribute (string, integer, ...)
```

Here's how you can get information about the relations

```
// returns the name of the first relation, eg. `author`
$modelInfo->relations->first()->name;

// returns the type of the
// first relation, eg. `BelongsTo`
$modelInfo->relations->first()->type;

// returns the related model of the
// first relation, eg. `App\Models\User`
$modelInfo->relations->first()->related;
```

Additionally, the package can also discover all the models in your application.

```
use Spatie\ModelInfo\ModelFinder;

// returns a `Illuminate\Support\Collection` containing all
// the class names of all your models.
$models = ModelFinder::all();
```

Support us
----------

[](#support-us)

[![](https://camo.githubusercontent.com/9e7e6f171214321723685abef411f016b70a2c5f3e2ce1e0c34ef31095d65cae/68747470733a2f2f6769746875622d6164732e73332e65752d63656e7472616c2d312e616d617a6f6e6177732e636f6d2f6c61726176656c2d6d6f64656c2d696e666f2e6a70673f743d31)](https://spatie.be/github-ad-click/laravel-model-info)

We invest a lot of resources into creating [best in class open source packages](https://spatie.be/open-source). You can support us by [buying one of our paid products](https://spatie.be/open-source/support-us).

We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on [our contact page](https://spatie.be/about-us). We publish all received postcards on [our virtual postcard wall](https://spatie.be/open-source/postcards).

Installation
------------

[](#installation)

You can install the package via composer:

```
composer require spatie/laravel-model-info
```

Usage
-----

[](#usage)

You can get information about a model by calling `forModel`:

```
use Spatie\ModelInfo\ModelInfo;

$modelInfo = ModelInfo::forModel(YourModel::class);

$modelInfo->fileName; // returns the filename that contains your model
$modelInfo->tableName; // returns the name of the table your models are stored in
$modelInfo->attributes; // returns a collection of `Attribute` objects
$modelInfo->relations; // returns a collection of `Relation` objects
```

> **Note**
>
> This package discovers relationships by their return type. Make sure that, in all your models each method that returns a relation has a return type.

### Getting a specific attribute

[](#getting-a-specific-attribute)

```
use Spatie\ModelInfo\ModelInfo;

// returns an instance of `Spatie\ModelInfo\Attributes\Attribute`
ModelInfo::forModel(BlogPost::class)->attribute('name');
```

### Getting a specific relation

[](#getting-a-specific-relation)

You can get a specific relation using the `relation` method.

```
use Spatie\ModelInfo\ModelInfo;

// returns an instance of `Spatie\ModelInfo\Relations\Relation`
ModelInfo::forModel(BlogPost::class)->relation('user')
```

### Attributes

[](#attributes)

A `Spatie\ModelInfo\Attributes\Attribute` object has these properties:

- `name`
- `type`
- `increments`
- `nullable`
- `default`
- `unique`
- `fillable`
- `appended`
- `cast`
- `virtual`

### Relationships

[](#relationships)

A `Spatie\ModelInfo\Relations\Relation` object has these properties:

- `name`
- `type`
- `related`

It also has a `relatedModelInfo()` method that gives a `ModelInfo` instance for the related model.

```
use Spatie\ModelInfo\ModelInfo;

ModelInfo::forModel(BlogPost::class)
   ->relation('user')
   ->relatedModelInfo() // returns the `ModelInfo` for the `User` model
```

Discovering all models in your application
------------------------------------------

[](#discovering-all-models-in-your-application)

Using this method we'll discover all methods in your project, no matter in which directory they are stored.

```
use Spatie\ModelInfo\ModelFinder;

// returns a `Illuminate\Support\Collection` containing
// all the class names of all your models.
$models = ModelFinder::all();
```

Getting information on all model in your application
----------------------------------------------------

[](#getting-information-on-all-model-in-your-application)

The `ModelInfo` class can get information about all models in your application.

```
use Spatie\ModelInfo\ModelInfo;

ModelInfo::forAllModels(); // returns a collection of `ModelInfo` instances
```

Adding extra info on a model
----------------------------

[](#adding-extra-info-on-a-model)

To add extra info on a model, add a method `extraModelInfo` to your model. It can return anything you want: an string, an object, an array.

```
// in your model

public function extraModelInfo()
{
    return 'anything you want';
}
```

The returned value will be available on the `extra` property of a `ModelInfo` instance.

```
use Spatie\ModelInfo\ModelInfo;

$modelInfo = ModelInfo::forModel(YourModel::class);

$modelInfo->extra; // returns 'anything you want'
```

Testing
-------

[](#testing)

```
composer test
```

Changelog
---------

[](#changelog)

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

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

[](#contributing)

Please see [CONTRIBUTING](https://github.com/freekmurze/.github/blob/main/CONTRIBUTING.md) for details.

Security Vulnerabilities
------------------------

[](#security-vulnerabilities)

Please review [our security policy](../../security/policy) on how to report security vulnerabilities.

Credits
-------

[](#credits)

- [Freek Van der Herten](https://github.com/freekmurze)
- [All Contributors](../../contributors)

This package contains code taken from the `model:show` command of [Laravel](https://github.com/laravel/framework).

License
-------

[](#license)

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

###  Health Score

62

—

FairBetter than 99% of packages

Maintenance84

Actively maintained with recent releases

Popularity53

Moderate usage in the ecosystem

Community31

Small or concentrated contributor base

Maturity69

Established project with proven stability

 Bus Factor1

Top contributor holds 66.3% 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 ~59 days

Recently: every ~180 days

Total

23

Last Release

80d ago

Major Versions

0.0.5 → 1.0.02022-09-07

1.4.4 → 2.0.02024-03-04

PHP version history (2 changes)0.0.1PHP ^8.1

2.0.0PHP ^8.2

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/7535935?v=4)[Spatie](/maintainers/spatie)[@spatie](https://github.com/spatie)

---

Top Contributors

[![freekmurze](https://avatars.githubusercontent.com/u/483853?v=4)](https://github.com/freekmurze "freekmurze (136 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (18 commits)")[![Riley19280](https://avatars.githubusercontent.com/u/14127031?v=4)](https://github.com/Riley19280 "Riley19280 (12 commits)")[![github-actions[bot]](https://avatars.githubusercontent.com/in/15368?v=4)](https://github.com/github-actions[bot] "github-actions[bot] (12 commits)")[![laravel-shift](https://avatars.githubusercontent.com/u/15991828?v=4)](https://github.com/laravel-shift "laravel-shift (6 commits)")[![pikant](https://avatars.githubusercontent.com/u/24542171?v=4)](https://github.com/pikant "pikant (5 commits)")[![clementbirkle](https://avatars.githubusercontent.com/u/9021081?v=4)](https://github.com/clementbirkle "clementbirkle (4 commits)")[![thecaliskan](https://avatars.githubusercontent.com/u/13554944?v=4)](https://github.com/thecaliskan "thecaliskan (2 commits)")[![iDevelopThings](https://avatars.githubusercontent.com/u/4105581?v=4)](https://github.com/iDevelopThings "iDevelopThings (2 commits)")[![Nielsvanpach](https://avatars.githubusercontent.com/u/10651054?v=4)](https://github.com/Nielsvanpach "Nielsvanpach (2 commits)")[![AlexVanderbist](https://avatars.githubusercontent.com/u/6287961?v=4)](https://github.com/AlexVanderbist "AlexVanderbist (2 commits)")[![patrickweh](https://avatars.githubusercontent.com/u/40495041?v=4)](https://github.com/patrickweh "patrickweh (1 commits)")[![hafezdivandari](https://avatars.githubusercontent.com/u/56585913?v=4)](https://github.com/hafezdivandari "hafezdivandari (1 commits)")[![ArtisanXL](https://avatars.githubusercontent.com/u/24480688?v=4)](https://github.com/ArtisanXL "ArtisanXL (1 commits)")[![patrickcurl](https://avatars.githubusercontent.com/u/1470061?v=4)](https://github.com/patrickcurl "patrickcurl (1 commits)")

---

Tags

eloquentlaravelmodelsphpreflectionspatielaravellaravel-model-info

###  Code Quality

TestsPest

Code StyleLaravel Pint

### Embed Badge

![Health badge](/badges/spatie-laravel-model-info/health.svg)

```
[![Health](https://phpackages.com/badges/spatie-laravel-model-info/health.svg)](https://phpackages.com/packages/spatie-laravel-model-info)
```

###  Alternatives

[spatie/laravel-permission

Permission handling for Laravel 12 and up

12.9k89.8M1.0k](/packages/spatie-laravel-permission)[spatie/laravel-medialibrary

Associate files with Eloquent models

6.1k37.7M472](/packages/spatie-laravel-medialibrary)[spatie/laravel-activitylog

A very simple activity logger to monitor the users of your website or application

5.8k45.4M309](/packages/spatie-laravel-activitylog)[spatie/laravel-health

Monitor the health of a Laravel application

86910.0M83](/packages/spatie-laravel-health)[dyrynda/laravel-model-uuid

This package allows you to easily work with UUIDs in your Laravel models.

4802.8M8](/packages/dyrynda-laravel-model-uuid)[spatie/laravel-translation-loader

Store your language lines in the database, yaml or other sources

8362.9M51](/packages/spatie-laravel-translation-loader)

PHPackages © 2026

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