A Laravel package to auto-generate models and migrations from table definitions.
Noouh For Integrated Solutions is dedicated to providing innovative software solutions. We specialize in developing high-quality applications tailored to meet the specific needs of our clients.
To install the Noouh Auto Model package, you can require it using Composer:
composer require noouh/auto-modelAfter installing the package, publish the stubs using the following command:
php artisan vendor:publish --tag=stubsThis will publish the model.stub and migration.stub files to your project's stubs directory.
Create a JSON file (e.g., tables.json) with your table definitions and relationships. Here is an example structure:
[
{
"table": "users",
"columns": [
{ "name": "name", "type": "string" },
{ "name": "email", "type": "string" }
]
},
{
"table": "posts",
"columns": [
{ "name": "title", "type": "string" },
{ "name": "content", "type": "text" },
{ "name": "user_id", "type": "unsignedBigInteger" }
],
"relationships": [
{ "type": "belongsTo", "relatedTable": "users", "foreignKey": "user_id" }
]
}
]Run the command to generate models and migrations:
php artisan generate:models /path/to/tables.jsonGiven the following tables.json file:
[
{
"table": "users",
"columns": [
{ "name": "name", "type": "string" },
{ "name": "email", "type": "string" }
]
},
{
"table": "posts",
"columns": [
{ "name": "title", "type": "string" },
{ "name": "content", "type": "text" },
{ "name": "user_id", "type": "unsignedBigInteger" }
],
"relationships": [
{ "type": "belongsTo", "relatedTable": "users", "foreignKey": "user_id" }
]
}
]Running the command:
php artisan generate:models /path/to/tables.jsonWill generate:
- A
Usermodel inapp/Models/User.php - A
Postmodel inapp/Models/Post.phpwith abelongsTorelationship to theUsermodel - A migration file for the
userstable - A migration file for the
poststable with a foreign key to theuserstable
This package is open-sourced software licensed under the MIT license.