fariddomat / auto-api
Automates RESTful API generation for Laravel.
Installs: 2
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Type:laravel-package
Requires
- php: >=8
This package is auto-updated.
Last update: 2025-06-05 12:22:53 UTC
README
Auto-API is a Laravel package that automates the generation of RESTful API endpoints for your models with a single interactive command. It creates models, migrations, controllers, routes, and OpenAPI specifications, supporting features like file uploads, pagination, soft deletes, searchable fields, and relationship data for select
fields—all with a colorful CLI experience.
Installation
Install Auto-API via Composer:
composer require fariddomat/auto-api:dev-main
No additional setup is required—the package auto-registers its command via the service provider.
Usage
Generate an API Module
Use the interactive make:auto-api Artisan command to generate an API module:
php artisan make:auto-api
Interactive Example
Welcome to AutoAPI Generator! Let's create your API step-by-step. Model name? (e.g., Post; must start with a capital letter): Product Define fields for Product (e.g., title:string, user_id:select). Leave blank to finish. Enter a field: name:string Enter a field: category_id:select Enter a field: image:image Enter a field: <Enter> API version? (e.g., v1, v2; default: v1): v1 Enable soft deletes? (Default: No) [N/y]: y Enable search functionality? (Default: No) [N/y]: y Searchable fields (comma-separated, from: name, category_id, image): name Middleware (comma-separated, e.g., auth:api,throttle; leave blank for none): auth:api API Settings: Model: Product Fields: name:string, category_id:select, image:image Version: v1 Soft Deletes: Yes Search Enabled: Yes Searchable Fields: name Middleware: auth:api Proceed with these settings? [Y/n]: <Enter> Generating Auto API for Product...
This generates:
- Model: Product with $fillable, $searchable, validation rules(), and relationships.
- Migration: products table with id, name, category_id (foreign key), image, deleted_at, timestamps.
- Controller: ProductApiController with RESTful methods (index, create, store, show, edit, update, destroy, restore).
- Routes: API routes in routes/api.php under v1 with auth:api middleware.
- OpenAPI: openapi/Product.json spec documenting all endpoints.
Fields
Define fields interactively with these supported types:
Field Type | Description | Example Usage |
---|---|---|
string | Text input | name:string |
decimal | Decimal number (e.g., prices) | price:decimal |
integer | Integer number | quantity:integer |
text | Longer text field | description:text |
select | Foreign key with relationship | user_id:select |
boolean | True/false value | is_active:boolean |
file | Single file upload (e.g., PDF) | manual:file |
image | Single image upload | thumbnail:image |
images | Multiple image uploads | gallery:images |
Modifiers like :nullable can be appended (e.g., name:string:nullable).
Features
✔ Interactive CLI: Generate APIs with a colorful, step-by-step interface.
✔ Automatic API Generation: Creates models, migrations, controllers, routes, and OpenAPI specs.
✔ Customizable Fields: Supports various data types, including relationships and file uploads.
✔ Pagination: index endpoint returns paginated results (default 15 per page).
✔ Searchable Fields: Filter index results with a search query parameter.
✔ Soft Deletes: Optional soft delete support with a restore endpoint.
✔ File Uploads: Handles file, image, and images fields with ImageHelper or Laravel storage.
✔ Relationships: Returns related model data (e.g., users) in create and edit responses.
✔ Middleware: Optional middleware application (e.g., auth:api, throttle).
✔ OpenAPI Spec: Generates a detailed OpenAPI 3.0 specification for each API.
API Endpoints
Method | Endpoint | Description |
---|---|---|
GET | /v1/{model} | List all records (paginated) |
GET | /v1/{model}/create | Get data for creating a record |
POST | /v1/{model} | Create a new record |
GET | /v1/{model}/{id} | Show a specific record |
GET | /v1/{model}/{id}/edit | Get data for editing a record |
PUT | /v1/{model}/{id} | Update a specific record |
DELETE | /v1/{model}/{id} | Delete a specific record |
POST | /v1/{model}/{id}/restore | Restore a soft-deleted record |
Pagination
- Use ?per_page={n}&page={m} to adjust results (e.g., GET /v1/products?per_page=10&page=2).
Search
- Use ?search={term} to filter results (e.g., GET /v1/products?search=phone).
File Uploads
- Use multipart/form-data for POST and PUT requests with file fields:
bash
curl -X POST /v1/products \ -F "name=Phone" \ -F "image=@phone.jpg" \ -F "gallery[]=@img1.jpg" \ -F "gallery[]=@img2.jpg"
File & Image Handling
Single File Upload (file)
Stores files in storage/app/public/uploads/{model}:
if ($request->hasFile('manual')) {
$validated['manual'] = $request->file('manual')->store('uploads/products', 'public');
}
Single Image Upload (image)
Uses ImageHelper if available, otherwise falls back to storage:
if ($request->hasFile('image') && class_exists('App\Helpers\ImageHelper')) {
$validated['image'] = ImageHelper::storeImageInPublicDirectory($request->file('image'), 'uploads/products');
} elseif ($request->hasFile('image')) {
$validated['image'] = $request->file('image')->store('uploads/products', 'public');
}
Multiple Image Uploads (images)
Processes multiple files into a JSON-encoded array:
if ($request->hasFile('gallery') && class_exists('App\Helpers\ImageHelper')) {
$validated['gallery'] = [];
foreach ($request->file('gallery') as $image) {
$validated['gallery'][] = ImageHelper::storeImageInPublicDirectory($image, 'uploads/products');
}
$validated['gallery'] = json_encode($validated['gallery']);
} elseif ($request->hasFile('gallery')) {
$validated['gallery'] = json_encode(array_map(fn($file) => $file->store('uploads/products', 'public'), $request->file('gallery')));
}
Configuration
- Customize Generated Files:
- Edit models in app/Models/.
- Modify controllers in app/Http/Controllers/.
- Adjust migrations in database/migrations/.
- Update the OpenAPI spec in openapi/.
- File Storage: Configure Laravel’s public disk or use ImageHelper for custom file handling.
Contribution
Contributions are welcome! Submit issues, bug fixes, or pull requests via GitHub to improve Auto-API.
License
Auto-API is licensed under the MIT License. See the LICENSE file for details.
text