{"id":22123500,"url":"https://github.com/proai/eloquent-inheritance","last_synced_at":"2026-03-05T06:01:30.810Z","repository":{"id":172544607,"uuid":"649271129","full_name":"ProAI/eloquent-inheritance","owner":"ProAI","description":":dna: Single table inheritance for Laravel inspired by the Rails implementation","archived":false,"fork":false,"pushed_at":"2025-05-29T15:39:40.000Z","size":12,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-12-08T10:45:27.216Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"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/ProAI.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","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,"zenodo":null}},"created_at":"2023-06-04T10:37:15.000Z","updated_at":"2025-05-29T15:38:32.000Z","dependencies_parsed_at":null,"dependency_job_id":"1986ad9b-efc7-4ba4-93e6-4a5db79d90ff","html_url":"https://github.com/ProAI/eloquent-inheritance","commit_stats":null,"previous_names":["proai/eloquent-inheritance"],"tags_count":7,"template":false,"template_full_name":null,"purl":"pkg:github/ProAI/eloquent-inheritance","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ProAI%2Feloquent-inheritance","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ProAI%2Feloquent-inheritance/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ProAI%2Feloquent-inheritance/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ProAI%2Feloquent-inheritance/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ProAI","download_url":"https://codeload.github.com/ProAI/eloquent-inheritance/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ProAI%2Feloquent-inheritance/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":30111779,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-03-05T03:40:26.266Z","status":"ssl_error","status_checked_at":"2026-03-05T03:39:15.902Z","response_time":93,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":[],"created_at":"2024-12-01T15:33:57.722Z","updated_at":"2026-03-05T06:01:30.430Z","avatar_url":"https://github.com/ProAI.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Eloquent Inheritance\n\n[![Latest Stable Version](https://poser.pugx.org/proai/eloquent-inheritance/v/stable)](https://packagist.org/packages/proai/eloquent-inheritance) [![Total Downloads](https://poser.pugx.org/proai/eloquent-inheritance/downloads)](https://packagist.org/packages/proai/eloquent-inheritance) [![Latest Unstable Version](https://poser.pugx.org/proai/eloquent-inheritance/v/unstable)](https://packagist.org/packages/proai/eloquent-inheritance) [![License](https://poser.pugx.org/proai/eloquent-inheritance/license)](https://packagist.org/packages/proai/eloquent-inheritance)\n\nSingle table inheritance is a way to emulate object-oriented inheritance in a relational database. While other frameworks like Ruby on Rails have a built-in implementation for this pattern, Laravel has not. This package aims to provide an as easy as possible implementation for Laravel.\n\n## Installation\n\nYou can install the package via composer:\n\n```bash\ncomposer require proai/eloquent-inheritance\n```\n\nPlease note that you need at least **PHP 8.0** and **Laravel 9** for this package.\n\n## Usage\n\nFirst you need to add the `Inheritance` trait to your root model:\n\n```php\nuse ProAI\\Inheritance\\Inheritance;\n\nclass Pet extends Model\n{\n    use Inheritance;\n\n    //\n}\n```\n\nThen you can extend the root model by other models that use the same table:\n\n```php\nclass Cat extends Pet\n{\n    //\n}\n\nclass Dog extends Pet\n{\n    //\n}\n```\n\n\u003e Note that you need a table `pets` with a column called `type` in order to make the example above work.\n\nWhenever a `Cat` or `Dog` model is instantiated, the attribute `type` will be set to the classname of the class (e.g. `App\\Models\\Cat`).\n\nOther than that there is no magic and a `Cat` or a `Dog` model will behave just like a normal Eloquent model. You can define cat or dog specific attributes and relationships on these models. An attribute only set for dogs for example should be a nullable column on the table, so that it is set for dogs but `null` for other pets.\n\n### Root model behaviour\n\nYou can query the root model like any other model. However, the returned models will be transformed based by the type name. For example if there is one cat and one dog in the database, `Pet::all();` will return one `Dog` and one `Cat` model.\n\nIt is not possible to use `new Pet($attributes);` when you specify the type in `$attributes`. Please use the static method `Pet::new($attributes)` instead. This method returns a new model based on the given type. For example if the type in `$attributes` is `App\\Models\\Cat`, it will return an instance of `App\\Models\\Cat`. If no type is specified, an instance of `App\\Models\\Pet` will be returned.\n\n### Custom type names\n\nIf you want to use different names for the type column than the classname, you can use the static `$inheritanceMap` property:\n\n```php\nclass Pet extends Model\n{\n    use Inheritance;\n\n    protected static $inheritanceMap = [\n        'cat' =\u003e Cat::class,\n        'dog' =\u003e Dog::class,\n    ];\n}\n```\n\n### Custom type column\n\nBy default the name of the type column is `type`. However, you can set a custom type column name:\n\n```php\nuse ProAI\\Inheritance\\Inheritance;\n\nclass Pet extends Model\n{\n    use Inheritance;\n\n    protected static $inheritanceColumn = 'pet_type';\n}\n```\n\n## Support\n\nBugs and feature requests are tracked on [GitHub](https://github.com/proai/eloquent-inheritance/issues).\n\n## License\n\nThis package is released under the [MIT License](LICENSE).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fproai%2Feloquent-inheritance","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fproai%2Feloquent-inheritance","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fproai%2Feloquent-inheritance/lists"}