fariddomat/auto-api
Laravel package that generates complete REST API modules via an interactive Artisan command: models, migrations, controllers, routes, and OpenAPI docs. Supports file uploads, pagination, soft deletes, searchable fields, and select-field relationships.
Installation
Run composer require fariddomat/auto-api:dev-main in your Laravel project. No additional configuration is required—the package auto-registers its command.
First Command
Execute php artisan make:auto-api to launch the interactive CLI. Follow prompts to define:
Product).name:string, category_id:select).v1).First Use Case
Generate a CRUD API for a Product model with name (string) and category_id (select). The package creates:
app/Models/Product.php, database/migrations/..._create_products_table.php).app/Http/Controllers/API/V1/ProductController.php).routes/api.php).api/openapi/v1/product.yaml).Iterative Development
Use make:auto-api repeatedly to scaffold APIs incrementally. For example:
name:string, price:decimal).image:image or description:text via the same command (fields are appended to existing migrations/models).Relationship Handling
For select fields (e.g., category_id:select), the package:
belongsTo relationship in the model.{ "category": { "id": 1, "name": "Electronics" } }).Category) first to avoid errors.API Versioning
Specify a version (e.g., v2) to generate routes under routes/api.php with versioned prefixes:
Route::prefix('v2')->group(function () {
// Auto-generated routes for v2
});
Middleware Integration
Assign middleware (e.g., auth:api) during generation. The package injects middleware into the route group:
Route::middleware(['auth:api'])->group(function () {
// Routes...
});
OpenAPI Documentation
Generated specs (api/openapi/v1/product.yaml) follow OpenAPI 3.0. Use tools like Swagger UI to visualize the API:
npm install @openapitools/openapi-generator-cli -g
openapi-generator-cli generate -i api/openapi/v1/product.yaml -g swagger-ui -o public/docs
Existing Models To extend an existing model, manually add fields to the migration/model, then regenerate the controller/routes via:
php artisan make:auto-api --model=Product --force
Note: The --force flag overwrites existing API files.
Custom Validation
Override the auto-generated controller’s rules() method to add custom validation:
public function rules()
{
return array_merge(parent::rules(), [
'price' => 'required|min:0',
]);
}
File Uploads
For image:image fields, the package handles:
storage/app/public).
Customize storage in config/filesystems.php or override the controller’s uploadPath() method.Search Functionality
Enable search with searchable_fields (e.g., name). The package adds a search query parameter to routes and implements search logic in the controller using Laravel’s where clauses.
Field Name Conflicts
Avoid reserved Laravel keywords (e.g., created_at) as field names. The package may fail silently or generate malformed migrations.
Relationship Loops
Circular relationships (e.g., Product has category_id:select and Category has product_id:select) can cause infinite recursion in API responses. Explicitly define with() in the controller or use hidden() in the model:
protected $hidden = ['pivot'];
Migration Conflicts
Regenerating a model’s API after manual migration changes may cause conflicts. Use --force cautiously:
php artisan make:auto-api --model=Product --force
Tip: Backup migrations before forcing updates.
OpenAPI Schema Issues
Complex relationships (e.g., polymorphic) may not render correctly in OpenAPI specs. Manually edit the generated YAML or extend the toOpenApiSchema() method in the model.
Middleware Overrides If middleware is added post-generation, update the route file manually or regenerate the API with the correct middleware flags.
CLI Errors
Check the package’s service provider (vendor/fariddomat/auto-api/src/ServiceProvider.php) for registration issues. Ensure no naming collisions with existing commands.
Route Conflicts
Auto-generated routes may clash with manual routes. Use php artisan route:list to debug and adjust route namespaces/prefixes.
File Upload Failures Verify:
image field’s column type in the migration (e.g., string for file paths).config/filesystems.php.public symlink exists (php artisan storage:link).Partial Regeneration To update only the controller/routes (not the model/migration), use:
php artisan make:auto-api --model=Product --skip-model
Testing
Auto-generated APIs include basic tests in tests/Feature/API/. Extend them with:
public function test_custom_validation()
{
$response = $this->postJson('/api/v1/products', [
'name' => 'Test',
'price' => -1, // Invalid
]);
$response->assertUnprocessable();
}
Extending Functionality Override the package’s generators by publishing and modifying its templates:
php artisan vendor:publish --tag=auto-api-templates
Edit files in resources/views/vendor/auto-api/.
Soft Deletes When enabled, the package adds:
deleted_at column to the migration.withTrashed() to queries in the controller.
Ensure your App\Models\Model extends Illuminate\Database\Eloquent\Model (not SoftDeletes directly).Performance
For large datasets, disable eager loading in the controller’s with() method or use loadMissing():
public function index()
{
return Product::with(['category'])->paginate(10);
// OR for lazy loading:
// return Product::paginate(10)->loadMissing('category');
}
How can I help you explore Laravel packages today?