Get Helpful Information about Models in Laravel

Packages

September 16th, 2022

Get Helpful Information about Models in Laravel

Laravel Model Info is a package by Spatie to get information on all the models in your Laravel project. This package is helpful if you're building functionality where you need to programmatically inspect models.

For example, you can access many important details like the database table name, attributes, relationships, and more:

1use Spatie\ModelInfo\ModelInfo;
2 
3$model = ModelInfo::for(Post::class);
4$model->attributes;
5$model->relations;
6// etc.
7 
8// Attributes and relations are collections
9$model->attributes->first()->name; // title
10$model->attributes->first()->type; // string(255)
11$model->attributes->first()->phpType; // string

One of the fantastic features I noticed in this package is getting all the models in your project:

1// Returns a collection of all your app's models
2$models = ModelFinder::all();

To learn more, check out Freek Van der Herten's article Getting information about all the models in your Laravel app. You can get documentation and the source code on GitHub at spatie/laravel-model-info.

Filed in:

Paul Redmond

Full stack web developer. Author of Lumen Programming Guide and Docker for PHP Developers.