Generate Laravel models, relations, and APIs from an interactive command line
marghoobsuleman/apiwizard is a Laravel package for generate laravel models, relations, and apis from an interactive command line.
It currently has 0 GitHub stars and 2 downloads on Packagist (latest version v1.0.1).
Install it with composer require marghoobsuleman/apiwizard.
Discover more Laravel packages by marghoobsuleman
or browse all Laravel packages to compare alternatives.
Last updated
A powerful Laravel package that generates models, relations, and complete REST APIs from an interactive command line interface or via command options.
Install the package via Composer:
composer require marghoobsuleman/apiwizard
The package will automatically register its service provider.
php artisan vendor:publish --tag=apiwizard-config
php artisan vendor:publish --tag=apiwizard-stubs
The package provides two commands (both work identically):
php artisan apiwizard:generate
# OR
php artisan modelwizard:generate
Run the command and follow the prompts:
php artisan apiwizard:generate
π§ 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!
Use command options for automation:
php artisan apiwizard:generate --table=users --relations=posts:hasMany --endpoint=/api/users --transform
| 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 |
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
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
];
}
}
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',
]);
}
}
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 userAfter 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,
];
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 templateAlways review generated code - The package creates a solid foundation, but you should review and customize:
Use transform methods - They provide a clean way to control API output without cluttering controllers
Leverage non-interactive mode - Perfect for:
Customize stubs - Publish and modify stubs to match your team's coding standards
Run the tests with:
composer test
Please see CHANGELOG for more information on what has changed recently.
Contributions are welcome! Please feel free to submit a Pull Request.
If you discover any security-related issues, please email [email protected] instead of using the issue tracker.
The MIT License (MIT). Please see License File for more information.
If you find this package helpful, please consider giving it a βοΈ on GitHub!
Made with β€οΈ for the Laravel community