yassin-ahmed/laravel-api-crud-generator 问题修复 & 功能扩展

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

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

yassin-ahmed/laravel-api-crud-generator

Composer 安装命令:

composer require yassin-ahmed/laravel-api-crud-generator

包简介

A comprehensive CRUD generator for Laravel API development

README 文档

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 field
  • sort_by - Field to sort by
  • sort_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

  1. Run migrations:

    php artisan migrate
  2. Register routes (if not using automatic discovery):

    // In routes/api.php
    require_once __DIR__ . '/api/Product.php';
  3. Run seeders (optional):

    php artisan db:seed --class=ProductSeeder
  4. 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:

  1. Check the issues page
  2. Create a new issue if your problem isn't already reported
  3. Provide detailed information about your Laravel version and the command you used

🙏 Credits

Created by Yassin

Made with ❤️ for the Laravel community

yassin-ahmed/laravel-api-crud-generator 适用场景与选型建议

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

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

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

围绕 yassin-ahmed/laravel-api-crud-generator 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

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