martink / apigenerator
A Laravel package to generate CRUD and API resources.
Requires
- php: ^8.0|^8.1|^8.2|^8.3|^8.4
- illuminate/database: ^10.0|^12.0
- illuminate/support: ^10.0|^12.0
README
ApiGenerator is a Laravel package that helps you generate a full CRUD stack (Model, Migration, Controller, Requests, Resource, Service, and API routes) and API scaffolding with a single Artisan command.
๐ Installation
You can install the package via Composer:
composer require martink/apigenerator --dev
If Laravel doesn't auto-discover the service provider, add it manually in config/app.php:
'providers' => [ MartinK\ApiGenerator\ApiGeneratorServiceProvider::class, ],
๐ฆ Publish (if needed)
php artisan vendor:publish --provider="MartinK\ApiGenerator\ApiGeneratorServiceProvider"
โ๏ธ Usage Generate a Full CRUD Stack
php artisan generate:crud ModelName --fields="title:string,description:text" --relationships="user:belongsTo"
This will generate:
app/Models/ModelName.php
database/migrations/xxxx_xx_xx_create_modelname_table.php
app/Http/Controllers/Api/ModelNameController.php
app/Http/Requests/ModelNameStoreRequest.php
app/Http/Requests/ModelNameUpdateRequest.php
app/Http/Resources/ModelNameResource.php
app/Services/ModelNameService.php
Route entry in routes/api.php
Generate API from Existing Models If your model already exists and includes $fillable, you can use:
php artisan generate:api
This command will loop through all models in app/Models and auto-generate:
Controllers
Resources
Services
Routes
๐งช Example
php artisan generate:crud Album --fields="name:string,coverImg:string,restaurant_id:foreignId" --relationships="restaurant:belongsTo,pictures:hasMany"
๐ Generated Controller Example
public function update(AlbumUpdateRequest $request, Album $album) { $updated = $this->service->update($album, $request->validated()); return new AlbumResource($updated); }
โ Requirements PHP ^7.4|^8.0
Laravel 8 or 9+
License
This package is open-sourced software licensed under the MIT license.
Developed by Martin Karadzinov