rahulp / crud-generator
A Laravel package for generating CRUD operations with Repository Pattern
Requires
- php: ^8.2
- laravel/framework: ^11.0
This package is auto-updated.
Last update: 2025-06-18 10:59:03 UTC
README
A powerful Laravel package that generates complete CRUD operations with Repository Pattern implementation, along with API response helpers and database transaction middleware.
Features
- 🚀 Project setup with essential middleware and helpers
- 🏗️ Generates Models, Controllers, Services, and Repositories
- 📦 Automatic repository binding setup
- 🔄 Database transaction middleware
- 📬 API response helper functions
- ⚡ Support for Laravel 11
- 🛠️ Customizable validation rules
Installation
You can install the package via composer:
composer require rahulp/crud-generator
Commands
1. Project Setup (Required First)
php artisan project:setup
This command sets up essential components needed for the CRUD operations:
A. Database Transaction Middleware
Automatically adds and registers middleware that:
- Wraps all API requests in database transactions
- Automatically commits on successful responses
- Rolls back on exceptions or error responses (4xx, 5xx)
- Handles exceptions gracefully
B. API Response Helpers
Adds global helper functions for consistent API responses:
- Success Response Helper:
ok($message = null, $data = [], $status = 200); // Examples: return ok('User profile fetched', $user); return ok('Post created', $post, 201);
Success Response Format:
{ "meta": { "status": 200, "message": "User profile fetched", "success": true }, "data": { "id": 1, "name": "John Doe", "email": "john@example.com" } }
- Error Response Helper:
error($message = null, $data = [], $type = null); // Examples: return error('Validation failed', $validator->errors(), 'validation'); return error('Post not found', [], 'notfound'); return error('Unauthorized access', [], 'forbidden');
Error Types and Status Codes:
validation
(422) - Validation errorsunauthenticated
(401) - Authentication requirednotfound
(404) - Resource not foundforbidden
(403) - Permission deniedprocessError
(400) - Bad requestloginCase
(306) - Login specific errors- Default (500) - Server errors
Error Response Format:
{ "meta": { "status": 422, "message": "Validation failed", "success": false }, "data": { "email": ["The email field is required"], "name": ["The name field is required"] } }
2. Generate CRUD Operations
After setting up the project, you can generate CRUD operations:
php artisan make:crud {model} {columns}
Parameters:
model
: Name of the model (e.g., Post, User, Product)columns
: Column definitions in format: name:type:required|nullable:default
Supported Column Types:
string
- String/varchar fieldsinteger
- Whole numbersdecimal
- Decimal numbers (prices, measurements)text
- Long text contentboolean
- True/false valuesdate
- Date onlydatetime
- Date and timetimestamp
- Timestamps
Examples:
- Basic Post Model:
php artisan make:crud Post title:string:required content:text:nullable
- Product with Default Values:
php artisan make:crud Product \ name:string:required \ price:decimal:required:0.00 \ description:text:nullable \ stock:integer:required:0 \ is_active:boolean:required:true
- User Model with Validation:
php artisan make:crud User \ name:string:required \ email:string:required:unique \ phone:string:nullable \ status:string:required:active
Each make:crud
command generates:
- Database migration
- Model with fillable fields and defaults
- Repository interface and implementation
- Service class for business logic
- API controller with validation
- Automatic repository binding
Generated Structure
For a Post model, it creates:
app/
├── Http/
│ ├── Controllers/
│ │ └── PostController.php
│ └── Middleware/
│ └── DBTransaction.php
├── Models/
│ └── Post.php
├── Repositories/
│ ├── Interfaces/
│ │ └── PostRepositoryInterface.php
│ └── PostRepository.php
├── Services/
│ └── PostService.php
└── Helpers/
└── functions.php
API Endpoints
Each CRUD generation creates these endpoints:
# List all records with pagination GET /api/posts # Create new record POST /api/posts # Get single record GET /api/posts/{id} # Update record PUT /api/posts/{id} # Delete record DELETE /api/posts/{id}
Security
If you discover any security related issues, please email rahulspathak17@gmail.com instead of using the issue tracker.
License
The MIT License (MIT). Please see License File for more information.