{"id":19325093,"url":"https://github.com/spatie/laravel-model-info","last_synced_at":"2025-05-15T11:05:51.791Z","repository":{"id":55063686,"uuid":"522848467","full_name":"spatie/laravel-model-info","owner":"spatie","description":"Get information on all the models in your Laravel project","archived":false,"fork":false,"pushed_at":"2025-02-21T11:45:44.000Z","size":167,"stargazers_count":175,"open_issues_count":0,"forks_count":21,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-05-07T23:03:53.781Z","etag":null,"topics":["eloquent","laravel","models","php","reflection"],"latest_commit_sha":null,"homepage":"https://freek.dev/2332-getting-information-about-all-the-models-in-your-laravel-app","language":"PHP","has_issues":false,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/spatie.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-08-09T07:33:51.000Z","updated_at":"2025-04-05T12:02:14.000Z","dependencies_parsed_at":"2024-03-07T08:27:57.858Z","dependency_job_id":"36fb5d91-b48d-46e6-93b1-fbf566f05572","html_url":"https://github.com/spatie/laravel-model-info","commit_stats":{"total_commits":102,"total_committers":8,"mean_commits":12.75,"dds":0.5686274509803921,"last_synced_commit":"09aa0151653f5faa61229e0f2741424ec7be5bad"},"previous_names":[],"tags_count":22,"template":false,"template_full_name":"spatie/package-skeleton-laravel","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spatie%2Flaravel-model-info","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spatie%2Flaravel-model-info/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spatie%2Flaravel-model-info/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spatie%2Flaravel-model-info/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/spatie","download_url":"https://codeload.github.com/spatie/laravel-model-info/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253446072,"owners_count":21909898,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["eloquent","laravel","models","php","reflection"],"created_at":"2024-11-10T02:08:51.461Z","updated_at":"2025-05-15T11:05:51.746Z","avatar_url":"https://github.com/spatie.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Get information about the models in your Laravel app\n\n[![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)\n[![Total Downloads](https://img.shields.io/packagist/dt/spatie/laravel-model-info.svg?style=flat-square)](https://packagist.org/packages/spatie/laravel-model-info)\n\nUsing this package you can determine which attributes and relations your model classes have.\n\n```php\nuse Spatie\\ModelInfo\\ModelInfo;\n\n$modelInfo = ModelInfo::forModel(YourModel::class);\n\n$modelInfo-\u003efileName; // returns the filename that contains your model\n$modelInfo-\u003etableName; // returns the name of the table your models are stored in\n$modelInfo-\u003eattributes; // returns a collection of `Attribute` objects\n$modelInfo-\u003erelations; // returns a collection of `Relation` objects\n```\n\nHere's how you can get information about the attributes:\n\n```php\n$modelInfo-\u003eattributes-\u003efirst()-\u003ename; // returns the name of the first attribute\n$modelInfo-\u003eattributes-\u003efirst()-\u003etype; // returns the type of the first attribute (string, integer, ...)\n```\n\nHere's how you can get information about the relations\n\n```php\n// returns the name of the first relation, eg. `author`\n$modelInfo-\u003erelations-\u003efirst()-\u003ename;\n\n// returns the type of the\n// first relation, eg. `BelongsTo`\n$modelInfo-\u003erelations-\u003efirst()-\u003etype;\n\n// returns the related model of the\n// first relation, eg. `App\\Models\\User`\n$modelInfo-\u003erelations-\u003efirst()-\u003erelated; \n```\n\nAdditionally, the package can also discover all the models in your application.\n\n```php\nuse Spatie\\ModelInfo\\ModelFinder;\n\n// returns a `Illuminate\\Support\\Collection` containing all\n// the class names of all your models.\n$models = ModelFinder::all(); \n```\n\n## Support us\n\n[\u003cimg src=\"https://github-ads.s3.eu-central-1.amazonaws.com/laravel-model-info.jpg?t=1\" width=\"419px\" /\u003e](https://spatie.be/github-ad-click/laravel-model-info)\n\nWe 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).\n\nWe 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).\n\n## Installation\n\nYou can install the package via composer:\n\n```bash\ncomposer require spatie/laravel-model-info\n```\n\n## Usage\n\nYou can get information about a model by calling `forModel`:\n\n```php\nuse Spatie\\ModelInfo\\ModelInfo;\n\n$modelInfo = ModelInfo::forModel(YourModel::class);\n\n$modelInfo-\u003efileName; // returns the filename that contains your model\n$modelInfo-\u003etableName; // returns the name of the table your models are stored in\n$modelInfo-\u003eattributes; // returns a collection of `Attribute` objects\n$modelInfo-\u003erelations; // returns a collection of `Relation` objects\n```\n\n\u003e **Note**\n\u003e \n\u003e 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.\n\n### Getting a specific attribute\n\n```php\nuse Spatie\\ModelInfo\\ModelInfo;\n\n// returns an instance of `Spatie\\ModelInfo\\Attributes\\Attribute`\nModelInfo::forModel(BlogPost::class)-\u003eattribute('name');\n```\n\n### Getting a specific relation\n\nYou can get a specific relation using the `relation` method.\n\n```php\nuse Spatie\\ModelInfo\\ModelInfo;\n\n// returns an instance of `Spatie\\ModelInfo\\Relations\\Relation`\nModelInfo::forModel(BlogPost::class)-\u003erelation('user')\n```\n\n### Attributes\n\nA `Spatie\\ModelInfo\\Attributes\\Attribute` object has these properties:\n\n- `name`\n- `type`\n- `increments`\n- `nullable`\n- `default`\n- `unique`\n- `fillable`\n- `appended`\n- `cast`\n- `virtual`\n\n### Relationships\n\nA `Spatie\\ModelInfo\\Relations\\Relation` object has these properties:\n\n- `name`\n- `type`\n- `related`\n\nIt also has a `relatedModelInfo()` method that gives a `ModelInfo` instance for the related model.\n\n```php\nuse Spatie\\ModelInfo\\ModelInfo;\n\nModelInfo::forModel(BlogPost::class)\n   -\u003erelation('user')\n   -\u003erelatedModelInfo() // returns the `ModelInfo` for the `User` model\n```\n\n## Discovering all models in your application\n\nUsing this method we'll discover all methods in your project, no matter in which directory they are stored.\n\n```php\nuse Spatie\\ModelInfo\\ModelFinder;\n\n// returns a `Illuminate\\Support\\Collection` containing\n// all the class names of all your models.\n$models = ModelFinder::all(); \n```\n\n## Getting information on all model in your application\n\nThe `ModelInfo` class can get information about all models in your application.\n\n```php\nuse Spatie\\ModelInfo\\ModelInfo;\n\nModelInfo::forAllModels(); // returns a collection of `ModelInfo` instances\n```\n\n## Adding extra info on a model\n\nTo 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.\n\n```php\n// in your model\n\npublic function extraModelInfo()\n{\n    return 'anything you want';\n}\n```\n\nThe returned value will be available on the `extra` property of a `ModelInfo` instance.\n\n```php\nuse Spatie\\ModelInfo\\ModelInfo;\n\n$modelInfo = ModelInfo::forModel(YourModel::class);\n\n$modelInfo-\u003eextra; // returns 'anything you want'\n```\n\n## Testing\n\n```bash\ncomposer test\n```\n\n## Changelog\n\nPlease see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.\n\n## Contributing\n\nPlease see [CONTRIBUTING](https://github.com/freekmurze/.github/blob/main/CONTRIBUTING.md) for details.\n\n## Security Vulnerabilities\n\nPlease review [our security policy](../../security/policy) on how to report security vulnerabilities.\n\n## Credits\n\n- [Freek Van der Herten](https://github.com/freekmurze)\n- [All Contributors](../../contributors)\n\nThis package contains code taken from the `model:show` command of [Laravel](https://github.com/laravel/framework).\n\n## License\n\nThe MIT License (MIT). Please see [License File](LICENSE.md) for more information.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fspatie%2Flaravel-model-info","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fspatie%2Flaravel-model-info","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fspatie%2Flaravel-model-info/lists"}