Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/spatie/laravel-model-info
Get information on all the models in your Laravel project
https://github.com/spatie/laravel-model-info
eloquent laravel models php reflection
Last synced: 4 days ago
JSON representation
Get information on all the models in your Laravel project
- Host: GitHub
- URL: https://github.com/spatie/laravel-model-info
- Owner: spatie
- License: mit
- Created: 2022-08-09T07:33:51.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-04-29T14:20:47.000Z (7 months ago)
- Last Synced: 2024-05-01T11:45:46.004Z (7 months ago)
- Topics: eloquent, laravel, models, php, reflection
- Language: PHP
- Homepage: https://freek.dev/2332-getting-information-about-all-the-models-in-your-laravel-app
- Size: 137 KB
- Stars: 165
- Watchers: 2
- Forks: 18
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.md
Awesome Lists containing this project
README
# Get information about the models in your Laravel app
[![Latest Version on Packagist](https://img.shields.io/packagist/v/spatie/laravel-model-info.svg?style=flat-square)](https://packagist.org/packages/spatie/laravel-model-info)
[![Total Downloads](https://img.shields.io/packagist/dt/spatie/laravel-model-info.svg?style=flat-square)](https://packagist.org/packages/spatie/laravel-model-info)Using this package you can determine which attributes and relations your model classes have.
```php
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:
```php
$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
```php
// 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.
```php
use Spatie\ModelInfo\ModelFinder;// returns a `Illuminate\Support\Collection` containing all
// the class names of all your models.
$models = ModelFinder::all();
```## Support us
[](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
You can install the package via composer:
```bash
composer require spatie/laravel-model-info
```## Usage
You can get information about a model by calling `forModel`:
```php
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
```php
use Spatie\ModelInfo\ModelInfo;// returns an instance of `Spatie\ModelInfo\Attributes\Attribute`
ModelInfo::forModel(BlogPost::class)->attribute('name');
```### Getting a specific relation
You can get a specific relation using the `relation` method.
```php
use Spatie\ModelInfo\ModelInfo;// returns an instance of `Spatie\ModelInfo\Relations\Relation`
ModelInfo::forModel(BlogPost::class)->relation('user')
```### Attributes
A `Spatie\ModelInfo\Attributes\Attribute` object has these properties:
- `name`
- `type`
- `increments`
- `nullable`
- `default`
- `unique`
- `fillable`
- `appended`
- `cast`
- `virtual`### 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.
```php
use Spatie\ModelInfo\ModelInfo;ModelInfo::forModel(BlogPost::class)
->relation('user')
->relatedModelInfo() // returns the `ModelInfo` for the `User` model
```## 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.
```php
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
The `ModelInfo` class can get information about all models in your application.
```php
use Spatie\ModelInfo\ModelInfo;ModelInfo::forAllModels(); // returns a collection of `ModelInfo` instances
```## 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.
```php
// in your modelpublic function extraModelInfo()
{
return 'anything you want';
}
```The returned value will be available on the `extra` property of a `ModelInfo` instance.
```php
use Spatie\ModelInfo\ModelInfo;$modelInfo = ModelInfo::forModel(YourModel::class);
$modelInfo->extra; // returns 'anything you want'
```## Testing
```bash
composer test
```## Changelog
Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.
## Contributing
Please see [CONTRIBUTING](https://github.com/freekmurze/.github/blob/main/CONTRIBUTING.md) for details.
## Security Vulnerabilities
Please review [our security policy](../../security/policy) on how to report security vulnerabilities.
## 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
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.