Package Data | |
---|---|
Maintainer Username: | spatie |
Maintainer Contact: | freek@spatie.be (Freek Van der Herten) |
Package Create Date: | 2022-08-09 |
Package Last Update: | 2024-09-23 |
Home Page: | https://freek.dev/2332-getting-information-about-all-the-models-in-your-laravel-app |
Language: | PHP |
License: | MIT |
Last Refreshed: | 2024-11-17 03:09:16 |
Package Statistics | |
---|---|
Total Downloads: | 165,055 |
Monthly Downloads: | 9,003 |
Daily Downloads: | 122 |
Total Stars: | 170 |
Total Watchers: | 2 |
Total Forks: | 19 |
Total Open Issues: | 1 |
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();
We invest a lot of resources into creating best in class open source packages. You can support us by buying one of our paid products.
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. We publish all received postcards on our virtual postcard wall.
You can install the package via composer:
composer require spatie/laravel-model-info
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.
use Spatie\ModelInfo\ModelInfo;
// returns an instance of `Spatie\ModelInfo\Attributes\Attribute`
ModelInfo::forModel(BlogPost::class)->attribute('name');
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')
A Spatie\ModelInfo\Attributes\Attribute
object has these properties:
name
type
increments
nullable
default
unique
fillable
appended
cast
virtual
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
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();
The ModelInfo
class can get information about all models in your application.
use Spatie\ModelInfo\ModelInfo;
ModelInfo::forAllModels(); // returns a collection of `ModelInfo` instances
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'
composer test
Please see CHANGELOG for more information on what has changed recently.
Please see CONTRIBUTING for details.
Please review our security policy on how to report security vulnerabilities.
This package contains code taken from the model:show
command of Laravel.
The MIT License (MIT). Please see License File for more information.