I am proud to announce that our group has launched a brand new small package deal: spatie/laravel-model-info. Let’s check out what this package deal can do.
This package deal makes it simple to find out your mannequin courses’ attributes and relations.
use SpatieModelInfoModelInfo; $modelInfo = ModelInfo::forModel(YourModel::class); // returns the filename that comprises your mannequin $modelInfo->fileName; // returns the title of the desk your fashions are saved in $modelInfo->tableName; // returns a set of `Attribute` objects $modelInfo->attributes; // returns a set of `Relation` objects $modelInfo->relations;
This is how one can get details about the attributes:
// returns the title of the primary attribute $modelInfo->attributes->first()->title; // returns the kind of the primary attribute (string, integer, ...) $modelInfo->attributes->first()->sort;
This is how one can get details about the relations
// returns the title of the primary relation, e.g. `writer` $modelInfo->relations->first()->title; // returns the kind of the // first relation, eg. `BelongsTo` $modelInfo->relations->first()->sort; // returns the associated mannequin of the // first relation, eg. `AppModelsUser` $modelInfo->relations->first()->associated;
Moreover, the package deal may also uncover all of the fashions in your utility.
use SpatieModelInfoModelFinder; // returns a `IlluminateSupportCollection` containing all // the category names of all of your fashions. $fashions = ModelFinder::all();
This is a screenshot of the package deal in motion on the code base of freek.dev
After we scroll down, we are able to see the attributes of the
Put up mannequin.
This package deal will probably be handy whenever you’re constructing tooling round Laravel that should know details about how the fashions seem like. We will probably be utilizing laravel-model-info ourselves below the hood in our laravel-data, typescript transformer, and upcoming php-type-graph packages.
This is not the primary package deal that our group has constructed. On our firm web site, take a look at all our open supply packages in this lengthy record. If you wish to help us, take into account selecting up any of our paid merchandise.