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

v1.0.1 2025-11-21 12:04 UTC

This package is auto-updated.

Last update: 2025-11-21 12:08:20 UTC


README

Latest Version on Packagist Total Downloads

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 user
  • GET /api/users/{id} - Show a specific user
  • PUT/PATCH /api/users/{id} - Update a user
  • DELETE /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 template
  • controller.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

  1. 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
  2. Use transform methods - They provide a clean way to control API output without cluttering controllers

  3. Leverage non-interactive mode - Perfect for:

    • CI/CD pipelines
    • Seeding multiple models
    • Scripted setups
  4. 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