yassin-ahmed / laravel-api-crud-generator
A comprehensive CRUD generator for Laravel API development
Installs: 38
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 0
Forks: 0
Open Issues: 0
pkg:composer/yassin-ahmed/laravel-api-crud-generator
Requires
- php: ^8.1|^8.2|^8.3
- laravel/framework: ^10.0|^11.0|^12.0
Requires (Dev)
- orchestra/testbench: ^8.0|^9.0|^10.0
- phpunit/phpunit: ^10.0|^11.0
README
A powerful Laravel package that generates complete CRUD APIs with a single command. This package creates everything you need for a fully functional REST API including models, controllers, requests, resources, migrations, factories, seeders, tests, and routes.
๐ Features
- Complete CRUD Generation: Models, Controllers, Requests, Resources, Migrations, Factories, Seeders, Tests, and Routes
- Field Type Support: String, text, integer, boolean, date, email, JSON, decimal, float, and more
- Relationship Support: BelongsTo and HasMany relationships
- Validation: Automatic validation rules generation based on field types
- API Resources: JSON API responses with proper formatting
- Pagination: Built-in pagination support with meta information
- Search & Filtering: Search functionality and sorting options
- Factory & Seeders: Automatic fake data generation for testing
- Feature Tests: Complete test suite for all CRUD operations
- Smart Fake Data: Context-aware fake data generation (emails, names, phones, etc.)
๐ฆ Installation
Install the package via Composer:
composer require yassin-ahmed/laravel-api-crud-generator
Publish the package configuration (optional):
php artisan vendor:publish --provider="Yassin\LaravelApiCrudGenerator\ServiceProvider"
๐ฏ Quick Start
Generate a complete CRUD API with a single command:
php artisan crud:generate Post --fields="title:string,content:text,published:boolean,published_at:date:nullable"
This creates:
- Migration file
- Post model
- PostController (API)
- Store/Update request classes
- PostResource
- API routes
- PostFactory
- PostSeeder
- Feature tests
๐ Usage
Basic Usage
# Simple model with basic fields php artisan crud:generate Product --fields="name:string,price:decimal,description:text" # With nullable fields php artisan crud:generate User --fields="name:string,email:email,phone:string:nullable,bio:text:nullable" # With relationships php artisan crud:generate Post --fields="title:string,content:text" --relations="belongsTo:User,hasMany:Comment"
Command Options
| Option | Description | Example |
|---|---|---|
--fields |
Define model fields with types | --fields="name:string,age:integer" |
--relations |
Define model relationships | --relations="belongsTo:User,hasMany:Post" |
--force |
Overwrite existing files | --force |
Field Types
| Type | Description | Validation Rule |
|---|---|---|
string |
Short text (255 chars) | string|max:255 |
text |
Long text | string |
integer |
Whole numbers | integer |
boolean |
True/false values | boolean |
date |
Date values | date |
email |
Email addresses | email |
json |
JSON data | json |
decimal |
Decimal numbers | numeric |
float |
Floating point numbers | numeric |
Relationship Types
| Type | Description | Example |
|---|---|---|
belongsTo |
Many-to-one relationship | belongsTo:User |
hasMany |
One-to-many relationship | hasMany:Comment |
๐ Examples
E-commerce Product
php artisan crud:generate Product \ --fields="name:string,description:text,price:decimal,stock:integer,is_active:boolean,category_id:integer" \ --relations="belongsTo:Category,hasMany:OrderItem"
Blog System
# Create Category php artisan crud:generate Category --fields="name:string,slug:string,description:text:nullable" # Create Post with relationship php artisan crud:generate Post \ --fields="title:string,slug:string,content:text,excerpt:text:nullable,published_at:date:nullable,is_published:boolean" \ --relations="belongsTo:User,belongsTo:Category,hasMany:Comment" # Create Comment php artisan crud:generate Comment \ --fields="content:text,author_name:string,author_email:email" \ --relations="belongsTo:Post"
User Management
php artisan crud:generate Profile \ --fields="first_name:string,last_name:string,bio:text:nullable,avatar:string:nullable,phone:string:nullable,date_of_birth:date:nullable" \ --relations="belongsTo:User"
๐ง Generated Files Structure
After running the command, the following files are created:
app/
โโโ Models/
โ โโโ YourModel.php
โโโ Http/
โ โโโ Controllers/Api/
โ โ โโโ YourModelController.php
โ โโโ Requests/YourModel/
โ โ โโโ StoreYourModelRequest.php
โ โ โโโ UpdateYourModelRequest.php
โ โโโ Resources/
โ โโโ YourModelResource.php
database/
โโโ migrations/
โ โโโ xxxx_xx_xx_xxxxxx_create_your_models_table.php
โโโ factories/
โ โโโ YourModelFactory.php
โโโ seeders/
โโโ YourModelSeeder.php
routes/api/
โโโ YourModel.php
tests/Feature/YourModel/
โโโ YourModelApiTest.php
๐ API Endpoints
The generated controller provides these endpoints:
| Method | Endpoint | Description |
|---|---|---|
GET |
/api/your-models |
List all records (with pagination) |
POST |
/api/your-models |
Create new record |
GET |
/api/your-models/{id} |
Show specific record |
PUT/PATCH |
/api/your-models/{id} |
Update record |
DELETE |
/api/your-models/{id} |
Delete record |
Query Parameters
search- Search in name fieldsort_by- Field to sort bysort_direction- Sort direction (asc/desc)per_page- Records per page (default: 15)
Example API Calls
# List products with pagination GET /api/products?per_page=10&page=1 # Search products GET /api/products?search=laptop # Sort products by price GET /api/products?sort_by=price&sort_direction=desc # Create product POST /api/products { "name": "Laptop", "price": 999.99, "description": "High-performance laptop" } # Update product PUT /api/products/1 { "name": "Gaming Laptop", "price": 1299.99 }
๐งช Testing
The package generates comprehensive feature tests. Run them with:
# Run all tests php artisan test # Run specific model tests php artisan test tests/Feature/Product/ProductApiTest.php # Run with coverage php artisan test --coverage
๐ After Generation
-
Run migrations:
php artisan migrate
-
Register routes (if not using automatic discovery):
// In routes/api.php require_once __DIR__ . '/api/Product.php';
-
Run seeders (optional):
php artisan db:seed --class=ProductSeeder
-
Customize as needed:
- Update validation rules in request classes
- Modify API resource fields
- Add custom methods to controllers
- Enhance factory definitions
โก Advanced Features
Custom Validation
Update the generated request classes to add custom validation:
// In StoreProductRequest.php public function rules() { return [ 'name' => 'required|string|max:255|unique:products', 'price' => 'required|numeric|min:0', 'description' => 'nullable|string|max:1000', ]; }
Custom API Resources
Enhance the generated resource classes:
// In ProductResource.php public function toArray($request) { return [ 'id' => $this->id, 'name' => $this->name, 'price' => number_format($this->price, 2), 'description' => $this->description, 'formatted_price' => '$' . number_format($this->price, 2), 'category' => new CategoryResource($this->whenLoaded('category')), 'created_at' => $this->created_at->format('Y-m-d H:i:s'), 'updated_at' => $this->updated_at->format('Y-m-d H:i:s'), ]; }
๐ค Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
๐ License
This package is open-sourced software licensed under the MIT license.
๐ Support
If you encounter any issues or have questions:
- Check the issues page
- Create a new issue if your problem isn't already reported
- Provide detailed information about your Laravel version and the command you used
๐ Credits
Created by Yassin
Made with โค๏ธ for the Laravel community