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-model
After installing the package, publish the stubs using the following command:
php artisan vendor:publish --tag=stubs
This 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.json
Given 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.json
Will generate:
- A
User
model inapp/Models/User.php
- A
Post
model inapp/Models/Post.php
with abelongsTo
relationship to theUser
model - A migration file for the
users
table - A migration file for the
posts
table with a foreign key to theusers
table
This package is open-sourced software licensed under the MIT license.