marghoobsuleman / apiwizard
Generate Laravel models, relations, and APIs from an interactive command line
Installs: 1
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 0
Forks: 0
Open Issues: 0
pkg:composer/marghoobsuleman/apiwizard
Requires
- php: ^8.2
- illuminate/console: ^11.0|^12.0
- illuminate/filesystem: ^11.0|^12.0
- illuminate/support: ^11.0|^12.0
- laravel/framework: 11.*
Requires (Dev)
- orchestra/testbench: ^9.0|^10.0
- phpunit/phpunit: ^10.0|^11.0|^12.0
- squizlabs/php_codesniffer: ^3.8
This package is auto-updated.
Last update: 2025-11-21 12:08:20 UTC
README
A powerful Laravel package that generates models, relations, and complete REST APIs from an interactive command line interface or via command options.
โจ Features
- ๐ฏ Interactive CLI - User-friendly prompts guide you through the generation process
- ๐ Quick Generation - Create models, controllers, and routes in seconds
- ๐ Automatic Relations - Define and generate model relationships effortlessly
- ๐ Safe Updates - Add relations to existing models without overwriting code
- ๐จ Data Transformation - Optional transform methods for customizing API responses
- ๐ฆ Complete REST API - Generates full CRUD endpoints automatically
- โ๏ธ Non-Interactive Mode - Use command options for automation and scripting
- ๐ ๏ธ Customizable - Publish and modify stubs to match your coding style
- ๐ PSR-4 Compliant - Follows Laravel and PHP best practices
๐ Requirements
- PHP 8.2 or higher
- Laravel 11.x or 12.x
๐ฆ Installation
Install the package via Composer:
composer require marghoobsuleman/apiwizard
The package will automatically register its service provider.
Publish Configuration (Optional)
php artisan vendor:publish --tag=apiwizard-config
Publish Stubs (Optional)
php artisan vendor:publish --tag=apiwizard-stubs
๐ Usage
Available Commands
The package provides two commands (both work identically):
php artisan apiwizard:generate
# OR
php artisan modelwizard:generate
Interactive Mode
Run the command and follow the prompts:
php artisan apiwizard:generate
Example Interactive Session:
๐ง APIWizard - Laravel API Generator
Enter table name: users
Model name will be: User
Does it have any relations? (yes/no): yes
Enter related table name: posts
Type of relation:
[0] hasOne
[1] hasMany
[2] belongsTo
[3] belongsToMany
> 1
โ Added hasMany relation with posts
Add another relation? (yes/no): no
Do you want to modify returned data (add transform method)? (yes/no): yes
Do you want to create an API endpoint? (yes/no): yes
Enter API endpoint [/api/users]: /api/users
๐จ Generating files...
โ Model created: /app/Models/User.php
โ Related model created: /app/Models/Post.php
โ Controller created: /app/Http/Controllers/API/UserController.php
โ Routes added to: routes/api.php
โ
Generation completed successfully!
Non-Interactive Mode
Use command options for automation:
php artisan apiwizard:generate --table=users --relations=posts:hasMany --endpoint=/api/users --transform
Available Options:
| Option | Description | Example |
|---|---|---|
--table |
Table name (required for non-interactive) | --table=users |
--relations |
Relations in format "table:type" (can be used multiple times) | --relations=posts:hasMany --relations=profile:hasOne |
--endpoint |
API endpoint path | --endpoint=/api/users |
--transform |
Include transform method in model | --transform |
--no-api |
Skip API generation (model only) | --no-api |
Examples:
Generate model with multiple relations:
php artisan apiwizard:generate \ --table=users \ --relations=posts:hasMany \ --relations=profile:hasOne \ --relations=roles:belongsToMany \ --endpoint=/api/users \ --transform
Generate model without API:
php artisan apiwizard:generate --table=products --no-api
Simple model with API:
php artisan apiwizard:generate --table=categories --endpoint=/api/categories
๐ What Gets Generated
1. Model File
Generated at app/Models/{ModelName}.php:
<?php namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; class User extends Model { use HasFactory; protected $table = 'users'; protected $fillable = [ // Add your fillable attributes here ]; // Relations public function posts() { return $this->hasMany(Post::class); } // Transform method (if requested) public function transform(): array { return [ 'id' => $this->id, // Add your custom transformations here ]; } }
2. Controller File
Generated at app/Http/Controllers/API/{ModelName}Controller.php:
<?php namespace App\Http\Controllers\API; use App\Models\User; use Illuminate\Http\Request; use App\Http\Controllers\Controller; class UserController extends Controller { public function index() { $users = User::paginate(15); return response()->json([ 'success' => true, 'data' => $users->map(fn($item) => $item->transform()), 'pagination' => [ 'total' => $users->total(), 'per_page' => $users->perPage(), 'current_page' => $users->currentPage(), 'last_page' => $users->lastPage(), ], ]); } public function show($id) { $user = User::findOrFail($id); return response()->json([ 'success' => true, 'data' => $user->transform(), ]); } public function store(Request $request) { $validated = $request->validate([ // Add your validation rules here ]); $user = User::create($validated); return response()->json([ 'success' => true, 'message' => 'User created successfully', 'data' => $user, ], 201); } public function update(Request $request, $id) { $user = User::findOrFail($id); $validated = $request->validate([ // Add your validation rules here ]); $user->update($validated); return response()->json([ 'success' => true, 'message' => 'User updated successfully', 'data' => $user, ]); } public function destroy($id) { $user = User::findOrFail($id); $user->delete(); return response()->json([ 'success' => true, 'message' => 'User deleted successfully', ]); } }
3. API Routes
Added to routes/api.php:
// UserController routes Route::apiResource('users', \App\Http\Controllers\API\UserController::class);
This creates the following endpoints:
GET /api/users- List all users (paginated)POST /api/users- Create a new userGET /api/users/{id}- Show a specific userPUT/PATCH /api/users/{id}- Update a userDELETE /api/users/{id}- Delete a user
โ๏ธ Configuration
After publishing the config file, you can customize:
return [ // Model namespace 'model_namespace' => 'App\\Models', // Controller namespace 'controller_namespace' => 'App\\Http\\Controllers\\API', // Model path 'model_path' => app_path('Models'), // Controller path 'controller_path' => app_path('Http/Controllers/API'), // Routes file 'routes_file' => base_path('routes/api.php'), // Default pagination 'pagination' => 15, ];
๐จ Customizing Stubs
Publish the stubs and modify them to match your preferences:
php artisan vendor:publish --tag=apiwizard-stubs
Stubs will be published to stubs/apiwizard/:
model.stub- Model templatecontroller.stub- Controller template
๐ Supported Relation Types
- hasOne - One-to-one relationship
- hasMany - One-to-many relationship
- belongsTo - Inverse of one-to-many
- belongsToMany - Many-to-many relationship
๐ก Tips & Best Practices
-
Always review generated code - The package creates a solid foundation, but you should review and customize:
- Add proper validation rules in controllers
- Define fillable/guarded attributes in models
- Customize transform methods for your API responses
-
Use transform methods - They provide a clean way to control API output without cluttering controllers
-
Leverage non-interactive mode - Perfect for:
- CI/CD pipelines
- Seeding multiple models
- Scripted setups
-
Customize stubs - Publish and modify stubs to match your team's coding standards
๐งช Testing
Run the tests with:
composer test
๐ Changelog
Please see CHANGELOG for more information on what has changed recently.
๐ค Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
๐ Security
If you discover any security-related issues, please email security@example.com instead of using the issue tracker.
๐ License
The MIT License (MIT). Please see License File for more information.
๐ Credits
๐ Support
If you find this package helpful, please consider giving it a โญ๏ธ on GitHub!
Made with โค๏ธ for the Laravel community