Oh Dear is the all-in-one monitoring tool for your entire website. We monitor uptime, SSL certificates, broken links, scheduled tasks and more. You'll get a notifications for us when something's wrong. All that paired with a developer friendly API and kick-ass documentation. O, and you'll also be able to create a public status page under a minute. Start monitoring using our free trial now.

Getting information about all the models in your Laravel app

Original – by Freek Van der Herten – 2 minute read

I'm proud to announce that our team has released a new small package: spatie/laravel-model-info. Let's take a look at what this package can do.

Using laravel-model-info

This package makes it easy to determine your model classes' attributes and relations.

use Spatie\ModelInfo\ModelInfo;

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

// returns the filename that contains your model
$modelInfo->fileName; 

// returns the name of the table your models are stored in
$modelInfo->tableName; 

// returns a collection of `Attribute` objects
$modelInfo->attributes; 

// returns a collection of `Relation` objects
$modelInfo->relations; 

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

// returns the name of the first attribute
$modelInfo->attributes->first()->name; 

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

Here's how you can get information about the relations

// returns the name of the first relation, e.g. `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(); 

Here's a screenshot of the package in action at the code base of freek.dev

screenshot

When we scroll down, we can see the attributes of the Post model.

screenshot

In closing

This package will be convenient when you're building tooling around Laravel that needs to know information about how the models look like. We will be using laravel-model-info ourselves under the hood in our laravel-data, typescript transformer, and upcoming php-type-graph packages.

This isn't the first package that our team has built. On our company website, check out all our open source packages in this long list. If you want to support us, consider picking up any of our paid products.

Stay up to date with all things Laravel, PHP, and JavaScript.

You can follow me on these platforms:

On all these platforms, regularly share programming tips, and what I myself have learned in ongoing projects.

Every month I send out a newsletter containing lots of interesting stuff for the modern PHP developer.

Expect quick tips & tricks, interesting tutorials, opinions and packages. Because I work with Laravel every day there is an emphasis on that framework.

Rest assured that I will only use your email address to send you the newsletter and will not use it for any other purposes.

Comments

What are your thoughts on "Getting information about all the models in your Laravel app"?

Comments powered by Laravel Comments
Want to join the conversation? Log in or create an account to post a comment.