rahulp/crud-generator
Composer 安装命令:
composer require rahulp/crud-generator
包简介
A Laravel package for generating CRUD operations with Repository Pattern
README 文档
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.
Credits
rahulp/crud-generator 适用场景与选型建议
rahulp/crud-generator 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 4 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 01 月 18 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 rahulp/crud-generator 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 rahulp/crud-generator 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 4
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 16
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-01-18