{"id":33963517,"url":"https://github.com/laracraft-tech/laravel-dynamic-model","last_synced_at":"2026-01-11T10:52:13.325Z","repository":{"id":59914503,"uuid":"540039566","full_name":"laracraft-tech/laravel-dynamic-model","owner":"laracraft-tech","description":"Dynamic Eloquent models for Laravel","archived":false,"fork":false,"pushed_at":"2025-08-12T06:16:34.000Z","size":101,"stargazers_count":44,"open_issues_count":2,"forks_count":2,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-09-18T00:53:16.524Z","etag":null,"topics":["dynamic-model","dynamic-schema","dynamic-table","eloquent","laravel","model","php"],"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/laracraft-tech.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,"zenodo":null}},"created_at":"2022-09-22T15:08:11.000Z","updated_at":"2025-08-12T06:16:32.000Z","dependencies_parsed_at":"2024-03-25T17:39:07.195Z","dependency_job_id":"02350ac4-42a8-4a2f-b594-a762faded304","html_url":"https://github.com/laracraft-tech/laravel-dynamic-model","commit_stats":{"total_commits":20,"total_committers":1,"mean_commits":20.0,"dds":0.0,"last_synced_commit":"311f3de7f40a50c15d0901609366bbe6c06e347f"},"previous_names":["sairahcaz/laravel-dynamic-model"],"tags_count":10,"template":false,"template_full_name":null,"purl":"pkg:github/laracraft-tech/laravel-dynamic-model","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/laracraft-tech%2Flaravel-dynamic-model","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/laracraft-tech%2Flaravel-dynamic-model/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/laracraft-tech%2Flaravel-dynamic-model/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/laracraft-tech%2Flaravel-dynamic-model/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/laracraft-tech","download_url":"https://codeload.github.com/laracraft-tech/laravel-dynamic-model/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/laracraft-tech%2Flaravel-dynamic-model/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":27693792,"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","status":"online","status_checked_at":"2025-12-12T02:00:06.775Z","response_time":129,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"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":["dynamic-model","dynamic-schema","dynamic-table","eloquent","laravel","model","php"],"created_at":"2025-12-12T22:40:54.089Z","updated_at":"2025-12-12T22:40:54.584Z","avatar_url":"https://github.com/laracraft-tech.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Dynamic Model for Laravel\n\n[![Latest Version on Packagist](https://img.shields.io/packagist/v/laracraft-tech/laravel-dynamic-model.svg?style=flat-square)](https://packagist.org/packages/laracraft-tech/laravel-dynamic-model)\n[![Tests](https://github.com/laracraft-tech/laravel-dynamic-model/actions/workflows/run-tests.yml/badge.svg?branch=main)](https://github.com/laracraft-tech/laravel-dynamic-model/actions/workflows/run-tests.yml)\n[![Check \u0026 fix styling](https://github.com/laracraft-tech/laravel-dynamic-model/actions/workflows/fix-php-code-style-issues.yml/badge.svg?branch=main)](https://github.com/laracraft-tech/laravel-dynamic-model/actions/workflows/fix-php-code-style-issues.yml)\n[![License](https://img.shields.io/packagist/l/laracraft-tech/laravel-dynamic-model.svg?style=flat-square)](https://packagist.org/packages/laracraft-tech/laravel-dynamic-model)\n\u003c!--[![Total Downloads](https://img.shields.io/packagist/dt/laracraft-tech/laravel-dynamic-model.svg?style=flat-square)](https://packagist.org/packages/laracraft-tech/laravel-dynamic-model)--\u003e\n\n\nNormally, each model in Laravel is written for only one table, and it's not so easy to break this convention.\nThis is for a good reason - it ensures a well-designed and clean model.\nBut in very specific cases, you may need to handle multiple tables via a single model.\nHere **Laravel Dynamic Model** comes into play!\nIt provides you with an eloquent model which finally can handle multiple tables\nand if you want also multiple database connections!\n\n## Installation\n\n### Dependencies\n\nThis package depends on Doctrine/DBAL, so make sure you have it installed.\n\n``` bash\ncomposer require doctrine/dbal\n```\n\n### Package\n\n``` bash\ncomposer require laracraft-tech/laravel-dynamic-model\n```\n\n## Usage\n\n### Let's create some dummy tables: \n\n---\n\n``` bash\nphp artisan make:migration create_foo_table\nphp artisan make:migration create_bar_table\n```\n\nCreate migrations for the tables:\n\n``` php\nSchema::create('foo', function (Blueprint $table) {\n    $table-\u003eid();\n    $table-\u003estring('col1');\n    $table-\u003einteger('col2');\n    $table-\u003etimestamps();\n});\n\nSchema::create('bar', function (Blueprint $table) {\n    $table-\u003edate('period')-\u003eprimary();\n    $table-\u003estring('col1');\n    $table-\u003einteger('col2');\n    $table-\u003etimestamps();\n});\n```\n\n``` bash\nphp artisan migrate\n```\n\n### Let's create a Dynamic Model\n\n---\n\nIf you want to create a **Dynamic Model** then you have to use the **DynamicModelFactory**.\nThe Factory ensures, that the `table` and optionally the `connection` gets set for your new created model.\nAlso it checks the schema of your provided table to set the propper values for: `primaryKey`, `keyType`, `incrementing`.\nMeans, also if you defined your table schema to have a primary key called for instance __period__ with a __date__ type, the Factory will handle it for you.\n\nNote that the default DynamicModel is set to **unguarded**.\nIf you do not like this or you want your Dynamic Models have some custom functions,\ncheck the section below and create your own Dynamic Model.\n\n``` php\nuse LaracraftTech\\LaravelDynamicModel\\DynamicModel;\nuse LaracraftTech\\LaravelDynamicModel\\DynamicModelFactory;\n\n$foo = app(DynamicModelFactory::class)-\u003ecreate(DynamicModel::class, 'foo');\n\n$foo-\u003ecreate([\n    'col1' =\u003e 'asdf',\n    'col2' =\u003e 123\n]);\n\n$faz = app(DynamicModelFactory::class)-\u003ecreate(DynamicModel::class, 'faz');\n\n$faz-\u003ecreate([\n    'period' =\u003e '2023-01-01',\n    'col1' =\u003e 'asdf',\n    'col2' =\u003e 123\n]);\n\n// optionally use another db connection (this one must be defined in your config/database.php file)\n$fooOtherDB = app(DynamicModelFactory::class)-\u003ecreate(DynamicModel::class, 'foo', 'mysql2');\n$fooOtherDB-\u003ecreate([...]);\n\ndump($foo-\u003efirst());\ndump($faz-\u003efirst());\ndump($fooOtherDB-\u003efirst());\n```\n\nWhich gives you:\n\n```\n^ LaracraftTech\\LaravelDynamicModel\\DynamicModel_mysql_foo {#328 ▼\n  #connection: \"mysql\"\n  #table: \"foo\"\n  #primaryKey: \"id\"\n  #keyType: \"int\"\n  +incrementing: true\n  #attributes: array:5 [▼\n    \"id\" =\u003e 1\n    \"col1\" =\u003e \"asdf\"\n    \"col2\" =\u003e 123\n    \"created_at\" =\u003e \"2023-03-22 15:34:22\"\n    \"updated_at\" =\u003e \"2023-03-22 15:34:22\"\n  ]\n}\n\n^ LaracraftTech\\LaravelDynamicModel\\DynamicModel_mysql_faz {#328 ▼\n  #connection: \"mysql\"\n  #table: \"faz\"\n  #primaryKey: \"period\"\n  #keyType: \"string\"\n  +incrementing: false\n  #attributes: array:5 [▼\n    \"period\" =\u003e \"2023-01-01\"\n    \"col1\" =\u003e \"asdf\"\n    \"col2\" =\u003e 123\n    \"created_at\" =\u003e \"2023-03-22 15:34:22\"\n    \"updated_at\" =\u003e \"2023-03-22 15:34:22\"\n  ]\n}\n\n^ LaracraftTech\\LaravelDynamicModel\\DynamicModel_mysql2_foo {#328 ▼\n  #connection: \"mysql2\"\n  #table: \"foo\"\n  #primaryKey: \"id\"\n  #keyType: \"int\"\n  +incrementing: true\n  #attributes: array:5 [▼\n    \"id\" =\u003e 1\n    \"col1\" =\u003e \"asdf\"\n    \"col2\" =\u003e 123\n    \"created_at\" =\u003e \"2023-03-22 15:34:22\"\n    \"updated_at\" =\u003e \"2023-03-22 15:34:22\"\n  ]\n}\n```\n\n### Use your own Dynamic Model\n\n---\n\nIf you need to add **custom** methods to your Dynamic Model or maybe **guard** it,\nyou can just create your own Eloquent model and then call it through the **Factory**.\nThis will create a new Model instance, which is **extended** by your original model.\nMake sure your model is implementing the **DynamicModelInterface** or is extended by the **DynamicModel** class.\n\n``` php\nnamespace App\\Models;\n\nuse Illuminate\\Database\\Eloquent\\Model;\nuse LaracraftTech\\LaravelDynamicModel\\DynamicModel;\nuse LaracraftTech\\LaravelDynamicModel\\DynamicModelInterface;\n\n// class MyDynamicModel extends Model implements DynamicModelInterface (this would also work)\nclass MyDynamicModel extends DynamicModel\n{\n    proteced $guarded = ['id'];\n\n    public function doSomething()\n    {\n        // do something\n    }\n}\n\n$foo = app(DynamicModelFactory::class)-\u003ecreate(MyDynamicModel::class, 'foo');\n\n$foo-\u003ecreate([\n    'col1' =\u003e 'asdf',\n    'col2' =\u003e 123\n]);\n\n$foo-\u003edoSomething();\n\ndd($foo-\u003efirst());\n```\n\nWhich gives you:\n\n```\n^ App\\Model\\MyDynamicModel_mysql_foo {#328 ▼\n  #connection: \"mysql\"\n  #table: \"foo\"\n  #primaryKey: \"id\"\n  #keyType: \"int\"\n  +incrementing: true\n  #attributes: array:5 [▼\n    \"id\" =\u003e 1\n    \"col1\" =\u003e \"asdf\"\n    \"col2\" =\u003e 123\n    \"created_at\" =\u003e \"2023-03-22 15:34:22\"\n    \"updated_at\" =\u003e \"2023-03-22 15:34:22\"\n  ]\n  ...\n}\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](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- [Zacharias Creutznacher](https://github.com/laracraft-tech)\n- [All Contributors](../../contributors)\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%2Flaracraft-tech%2Flaravel-dynamic-model","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flaracraft-tech%2Flaravel-dynamic-model","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flaracraft-tech%2Flaravel-dynamic-model/lists"}