mamunhoque/laravel-crud-builder 问题修复 & 功能扩展

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

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

mamunhoque/laravel-crud-builder

Composer 安装命令:

composer require mamunhoque/laravel-crud-builder

包简介

Advanced Laravel CRUD Builder - Automatically generate complete CRUD operations with intelligent migration parsing and code generation

README 文档

README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

An advanced Laravel package that automatically generates complete CRUD operations with intelligent migration parsing and sophisticated code generation. This package goes beyond simple scaffolding by analyzing your existing migration files and generating production-ready code that follows Laravel best practices.

Features

🚀 Intelligent Migration Parsing

  • Sophisticated analysis of migration files beyond simple regex
  • Handles complex column definitions, relationships, and constraints
  • Supports both PostgreSQL and MySQL/MariaDB syntax
  • Parses foreign keys, indexes, and enum values

🧠 Smart Code Generation

  • Generates appropriate validation rules based on column types and constraints
  • Creates dynamic search keys from text/varchar columns
  • Auto-detects and handles file upload fields
  • Generates API Resource classes for consistent response formatting
  • Implements relationship-aware filtering

🎯 Complete CRUD Stack

  • Models with fillable attributes, relationships, and accessors
  • Controllers with proper error handling and resource responses
  • Services with business logic and filtering capabilities
  • Form Requests with intelligent validation rules
  • API Resources for consistent data transformation
  • Factories with realistic fake data generation
  • Tests (Feature and Unit) with comprehensive coverage

⚙️ Advanced Configuration

  • Support for multiple middleware groups (admin, public, auth, custom)
  • Configurable route prefixes and naming conventions
  • Flexible file paths and namespaces
  • Soft deletes and timestamps support
  • Database compatibility for PostgreSQL, MySQL, and SQLite

Installation

You can install the package via Composer:

composer require mamunhoque/laravel-crud-builder

Publish the configuration file:

php artisan vendor:publish --tag="crud-builder-config"

Optionally, publish the stub files for customization:

php artisan vendor:publish --tag="crud-builder-stubs"

The package automatically publishes the HelperTrait to app/Traits/HelperTrait.php when installed. If you need to republish it:

php artisan crud-builder:publish-helper-trait

Quick Start

Basic Usage

Generate a complete CRUD for a model based on an existing migration:

php artisan make:advanced-crud Post

This will generate:

  • app/Models/Post.php
  • app/Http/Controllers/PostController.php
  • app/Services/PostService.php
  • app/Http/Requests/StorePostRequest.php
  • app/Http/Requests/UpdatePostRequest.php
  • app/Http/Resources/PostResource.php
  • database/factories/PostFactory.php
  • tests/Feature/PostControllerTest.php
  • tests/Unit/PostTest.php
  • Routes added to routes/api.php

Selective Generation

Generate only specific components:

# Generate only model and service
php artisan make:advanced-crud Post --model --service

# Generate everything except tests
php artisan make:advanced-crud Post --all --no-tests

Middleware and Route Configuration

# Generate for public API (no authentication)
php artisan make:advanced-crud Post --middleware=public

# Generate for authenticated users
php artisan make:advanced-crud Post --middleware=auth

# Custom route prefix
php artisan make:advanced-crud Post --prefix=v1/api

Individual Commands

Generate components individually:

# Generate model only
php artisan make:crud-model Post

# Generate service only
php artisan make:crud-service Post

# Generate controller only
php artisan make:crud-controller Post

# Generate request classes only
php artisan make:crud-request Post

# Generate API resource only
php artisan make:crud-resource Post

# Generate tests only
php artisan make:crud-test Post

Migration Requirements

The package analyzes your existing migration files. Ensure your migration follows Laravel conventions:

// Example migration: create_posts_table.php
Schema::create('posts', function (Blueprint $table) {
    $table->id();
    $table->string('title');
    $table->string('slug')->unique();
    $table->text('content')->nullable();
    $table->string('featured_image')->nullable();
    $table->enum('status', ['draft', 'published', 'archived'])->default('draft');
    $table->boolean('is_featured')->default(false);
    $table->decimal('price', 8, 2)->nullable();
    $table->foreignId('category_id')->constrained()->onDelete('cascade');
    $table->foreignId('user_id')->constrained()->onDelete('cascade');
    $table->json('metadata')->nullable();
    $table->timestamps();
    $table->softDeletes();
});

Generated Code Examples

Model with Relationships

class Post extends Model
{
    use HasFactory, SoftDeletes;

    protected $fillable = [
        'title', 'slug', 'content', 'featured_image', 'status', 
        'is_featured', 'price', 'category_id', 'user_id', 'metadata'
    ];

    protected $casts = [
        'is_featured' => 'boolean',
        'price' => 'decimal:2',
        'metadata' => 'array',
    ];

    protected $appends = ['featured_image_url'];

    public function category(): BelongsTo
    {
        return $this->belongsTo(Category::class);
    }

    public function user(): BelongsTo
    {
        return $this->belongsTo(User::class);
    }

    public function getFeaturedImageUrlAttribute(): ?string
    {
        if ($this->featured_image) {
            return config('filesystems.disks.s3.url') . '/' . $this->featured_image;
        }
        return null;
    }
}

Service with Intelligent Filtering

class PostService
{
    use HelperTrait;

    public function index($request): Collection|LengthAwarePaginator|array
    {
        $query = Post::query();
        
        $query->with(['category', 'user']);

        // Sorting
        $this->applySorting($query, $request);

        // Searching
        $searchKeys = ['title', 'content'];
        $this->applySearch($query, $request->input('search'), $searchKeys);

        // Apply filters
        if ($request->filled('category_id')) {
            $query->where('category_id', $request->input('category_id'));
        }
        
        if ($request->filled('status')) {
            $query->where('status', $request->input('status'));
        }

        return $this->paginateOrGet($query, $request);
    }
}

Smart Validation Rules

class StorePostRequest extends FormRequest
{
    public function rules(): array
    {
        return [
            'title' => 'required|string|max:255',
            'slug' => 'required|string|max:255|unique:posts,slug',
            'content' => 'nullable|string',
            'featured_image' => 'nullable|string',
            'status' => 'required|in:draft,published,archived',
            'is_featured' => 'nullable|boolean',
            'price' => 'nullable|numeric|min:0',
            'category_id' => 'required|exists:categories,id',
            'user_id' => 'nullable|exists:users,id',
            'metadata' => 'nullable|array',
        ];
    }
}

Configuration

The package is highly configurable. Here are some key configuration options:

// config/crud-builder.php
return [
    'defaults' => [
        'generate_model' => true,
        'generate_controller' => true,
        'generate_service' => true,
        'generate_requests' => true,
        'generate_resource' => true,
        'generate_tests' => true,
        'generate_routes' => true,
        'generate_factory' => true,
    ],

    'routes' => [
        'middleware_groups' => [
            'admin' => ['auth:api', 'role:admin'],
            'public' => [],
            'auth' => ['auth:api'],
        ],
        'default_middleware' => 'admin',
    ],

    'model' => [
        'use_soft_deletes' => true,
        'use_timestamps' => true,
        'generate_relationships' => true,
        'generate_accessors' => true,
    ],

    'validation' => [
        'generate_smart_rules' => true,
        'include_unique_rules' => true,
        'include_foreign_key_rules' => true,
    ],
];

Advanced Features

Relationship-Aware Filtering

The package automatically generates filters for relationships:

// Automatically generated in service
if ($request->filled('category_id')) {
    $query->where('category_id', $request->input('category_id'));
}

File Upload Detection

Automatically detects file upload fields and generates appropriate handling:

// In service
$data['featured_image'] = $this->s3FileUpload($request, 'featured_image', 'posts')['path'] ?? null;

// In model
public function getFeaturedImageUrlAttribute(): ?string
{
    if ($this->featured_image) {
        return config('filesystems.disks.s3.url') . '/' . $this->featured_image;
    }
    return null;
}

HelperTrait Features

The package includes a comprehensive HelperTrait with useful methods:

// Pagination and filtering
$this->applySorting($query, $request);
$this->applySearch($query, $searchValue, $searchKeys);
$this->paginateOrGet($query, $request);

// File uploads
$this->s3FileUpload($request, 'image', 'uploads');
$this->localFileUpload($request, 'file', 'documents');

// API responses
$this->successResponse($data, 'Success message');
$this->errorResponse($errors, 'Error message', 422);

// E-commerce specific filters
$this->applyEComFilters($query, $request);
$this->applyEComSorting($query, $request);

Comprehensive Testing

Generates both feature and unit tests:

// Feature test
public function it_can_create_post()
{
    $data = [
        'title' => fake()->sentence(),
        'content' => fake()->paragraph(),
        'status' => fake()->randomElement(['draft', 'published', 'archived']),
        'category_id' => Category::factory()->create()->id,
    ];

    $response = $this->postJson(route('admin.posts.store'), $data);

    $response->assertStatus(201);
    $this->assertDatabaseHas('posts', $data);
}

Requirements

  • PHP 8.1 or higher
  • Laravel 10.0 or higher
  • Existing migration files for the models you want to generate

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

Roadmap

  • Support for API versioning
  • GraphQL schema generation
  • Custom stub templates
  • Integration with Laravel Sanctum
  • Automatic API documentation generation
  • Support for polymorphic relationships
  • Database seeder generation
  • Integration with Laravel Horizon for queued operations

License

The MIT License (MIT). Please see License File for more information.

mamunhoque/laravel-crud-builder 适用场景与选型建议

mamunhoque/laravel-crud-builder 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 06 月 18 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「generator」 「rest」 「api」 「migration」 「crud」 「laravel」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 mamunhoque/laravel-crud-builder 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 mamunhoque/laravel-crud-builder 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

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