rahulp/crud-generator 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

rahulp/crud-generator

Composer 安装命令:

composer require rahulp/crud-generator

包简介

A Laravel package for generating CRUD operations with Repository Pattern

README 文档

README

Latest Version on Packagist License

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:

  1. 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"
    }
}
  1. 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 errors
  • unauthenticated (401) - Authentication required
  • notfound (404) - Resource not found
  • forbidden (403) - Permission denied
  • processError (400) - Bad request
  • loginCase (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 fields
  • integer - Whole numbers
  • decimal - Decimal numbers (prices, measurements)
  • text - Long text content
  • boolean - True/false values
  • date - Date only
  • datetime - Date and time
  • timestamp - Timestamps

Examples:

  1. Basic Post Model:
php artisan make:crud Post title:string:required content:text:nullable
  1. 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
  1. 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:

  1. Database migration
  2. Model with fillable fields and defaults
  3. Repository interface and implementation
  4. Service class for business logic
  5. API controller with validation
  6. 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 我们能提供哪些服务?
定制开发 / 二次开发

基于 rahulp/crud-generator 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 4
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 16
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-01-18