anwar/crud-generator

A powerful Laravel package for generating CRUD operations with advanced UI, validation, API generation, and comprehensive testing support. Database-safe with manual migration control.

2.1.0 2025-07-08 11:03 UTC

This package is auto-updated.

Last update: 2025-07-08 11:04:59 UTC


README

Latest Version License Total Downloads Tests

A powerful Laravel package that automates the creation of CRUD operations with advanced features including API generation, comprehensive validation support, and modern UI interface.

๐Ÿšจ Important Safety Notice

โš ๏ธ ALWAYS BACKUP YOUR DATABASE BEFORE INSTALLATION

This package includes migrations. For your safety:

  • Migrations are NOT automatically run
  • You must manually publish and review them
  • See SAFETY-NOTICE.md for complete safety guidelines

โœจ Features

  • ๐Ÿš€ Complete CRUD Generation - Models, Controllers, Views, Migrations, Requests, Resources
  • ๐ŸŽจ Modern UI Interface - Bootstrap 4 with live preview and advanced field configuration
  • โœ… Full Validation Support - All Laravel validation rules with visual builder
  • ๐Ÿ”— API Generation - RESTful controllers and JSON resources
  • ๐Ÿ“š Auto Documentation - Generate API docs in Markdown and HTML
  • โšก CLI Integration - Artisan commands for automation
  • ๐Ÿงช Test Generation - Feature and unit tests included
  • ๐ŸŽฏ Customizable Templates - Stub-based system for full customization
  • ๐Ÿ›ก๏ธ Database Safe - No automatic migrations, full user control

๐Ÿ“‹ Requirements

  • PHP 8.1 or higher
  • Laravel 10.0 or higher
  • MySQL 5.7+ or PostgreSQL 10+

๐Ÿš€ Quick Start

Safe Installation

# 1. BACKUP YOUR DATABASE FIRST!
mysqldump -u user -p database_name > backup.sql

# 2. Install the package
composer require anwar/crud-generator

# 3. Publish assets (migrations will NOT auto-run)
php artisan vendor:publish --tag=crudgenerator-config
php artisan vendor:publish --tag=crudgenerator-migrations
php artisan vendor:publish --tag=crudgenerator-assets

# 4. Review published migrations first!
ls database/migrations/*anwar*

# 5. Only run if safe
php artisan migrate

Access the Generator

Visit: http://your-app.com/admin/anwar-crud-generator

๐ŸŽฏ Usage Examples

Web Interface

  1. Access the Generator Interface

    http://your-app.com/admin/anwar-crud-generator
    
  2. Configure Your Module

    • Module Name: Post
    • Fields:
      • title (string, required|max:255)
      • content (text, required)
      • status (boolean)
      • published_at (datetime, nullable)
    • Relationships:
      • user (belongsTo User)
      • comments (hasMany Comment)
    • Options: โœ“ API, โœ“ Soft Deletes
  3. Preview and Generate

    • Use live preview to review generated code
    • Click "Generate" to create all files

CLI Commands

# Generate via Artisan
php artisan anwar-crud:generate Post \
  --fields="title:string,content:text,status:boolean" \
  --relationships="user:belongsTo" \
  --api \
  --soft-deletes

# List generated modules
php artisan anwar-crud:list

# Delete module
php artisan anwar-crud:delete Post --force

API Usage

# Generate via API
curl -X POST http://your-app.com/api/crud-modules \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer your-token" \
  -d '{
    "module": "Product",
    "fields": "name:string,price:decimal,description:text",
    "relationships": "category:belongsTo",
    "api": true,
    "softdeletes": false
  }'

# List modules
curl -X GET http://your-app.com/api/crud-modules \
  -H "Authorization: Bearer your-token"

๐Ÿ“ Generated Files

When you generate a Post module, you'll get:

app/
โ”œโ”€โ”€ Models/Post.php                    # Eloquent model with relationships
โ”œโ”€โ”€ Http/
โ”‚   โ”œโ”€โ”€ Controllers/
โ”‚   โ”‚   โ”œโ”€โ”€ PostController.php         # Web controller
โ”‚   โ”‚   โ””โ”€โ”€ Api/PostController.php     # API controller
โ”‚   โ”œโ”€โ”€ Requests/PostRequest.php       # Form validation
โ”‚   โ””โ”€โ”€ Resources/PostResource.php     # API resource
resources/views/posts/                 # Bootstrap 4 views
โ”œโ”€โ”€ index.blade.php                    # List view with DataTables
โ”œโ”€โ”€ create.blade.php                   # Create form
โ”œโ”€โ”€ edit.blade.php                     # Edit form
โ””โ”€โ”€ show.blade.php                     # Detail view
database/migrations/
โ””โ”€โ”€ create_posts_table.php             # Database migration
tests/
โ”œโ”€โ”€ Feature/PostTest.php               # Feature tests
โ””โ”€โ”€ Unit/PostTest.php                  # Unit tests

๐ŸŽจ Advanced Features

Field Types & Validation

Support for all Laravel field types and validation rules:

'fields' => [
    ['name' => 'email', 'type' => 'string', 'validation' => 'required|email|unique:users'],
    ['name' => 'age', 'type' => 'integer', 'validation' => 'required|integer|min:18|max:65'],
    ['name' => 'bio', 'type' => 'text', 'validation' => 'nullable|string|max:1000'],
    ['name' => 'avatar', 'type' => 'string', 'validation' => 'nullable|image|max:2048'],
    ['name' => 'settings', 'type' => 'json', 'validation' => 'nullable|array'],
    ['name' => 'salary', 'type' => 'decimal', 'validation' => 'required|numeric|min:0'],
]

Complex Relationships

'relationships' => [
    [
        'name' => 'user',
        'type' => 'belongsTo',
        'model' => 'User',
        'foreign_key' => 'user_id'
    ],
    [
        'name' => 'tags',
        'type' => 'belongsToMany',
        'model' => 'Tag',
        'pivot_table' => 'post_tags'
    ]
]

API Features

Generated API controllers include:

  • โœ… RESTful endpoints (GET, POST, PUT, DELETE)
  • โœ… JSON API resources with data transformation
  • โœ… Proper HTTP status codes
  • โœ… Error handling and validation
  • โœ… Pagination support
  • โœ… Rate limiting ready

Example API endpoints:

GET    /api/posts           # List all posts
POST   /api/posts           # Create new post
GET    /api/posts/{id}      # Get specific post
PUT    /api/posts/{id}      # Update post
DELETE /api/posts/{id}      # Delete post

๐Ÿ”ง Configuration

Customize the package behavior in config/anwarcrud.php:

return [
    'model_namespace' => 'App\\Models',
    'controller_namespace' => 'App\\Http\\Controllers',
    'view_path' => 'resources/views',
    
    'features' => [
        'api_generation' => true,
        'test_generation' => true,
        'documentation_generation' => true,
        'soft_deletes' => true,
    ],
    
    'ui' => [
        'theme' => 'bootstrap4',
        'show_preview' => true,
        'enable_live_preview' => true,
    ],
];

๐ŸŽฏ Customization

Custom Templates

  1. Publish stubs:

    php artisan vendor:publish --tag=anwar-crud-stubs
  2. Modify templates in resources/crud-stubs/

  3. Use custom placeholders:

    • @modelName - Model class name
    • @modelVar - Model variable name
    • @fields - Generated fields
    • @relationships - Generated relationships

Custom Field Types

Add custom field types:

'custom_fields' => [
    'phone' => [
        'migration_type' => 'string',
        'validation' => 'required|string|regex:/^[0-9\-\+\s\(\)]+$/',
        'input_type' => 'tel'
    ]
]

๐Ÿงช Testing

Run package tests:

vendor/bin/phpunit packages/CrudGenerator/tests/

Generated modules include comprehensive tests:

// Feature Test Example
public function test_can_create_post()
{
    $data = ['title' => 'Test Post', 'content' => 'Test Content'];
    
    $response = $this->post(route('posts.store'), $data);
    
    $response->assertRedirect(route('posts.index'));
    $this->assertDatabaseHas('posts', $data);
}

// API Test Example  
public function test_api_can_list_posts()
{
    Post::factory(3)->create();
    
    $response = $this->getJson('/api/posts');
    
    $response->assertOk()
             ->assertJsonCount(3, 'data');
}

๐Ÿ“– Documentation

  • Full Documentation: DOCUMENTATION.md
  • API Reference: Complete API documentation with examples
  • Video Tutorials: [Coming Soon]
  • Wiki: GitHub Wiki

๐Ÿค Contributing

We welcome contributions! Please see CONTRIBUTING.md for details.

Development Setup

git clone https://github.com/ringkubd/crud-generator.git
cd crud-generator
composer install
cp .env.example .env.testing
php artisan key:generate --env=testing
vendor/bin/phpunit

๐Ÿ› Bug Reports & Feature Requests

Please use GitHub Issues for:

  • ๐Ÿ› Bug reports
  • ๐Ÿ’ก Feature requests
  • ๐Ÿ“ Documentation improvements
  • โ“ Questions and support

๐Ÿ“„ License

This package is open-source software licensed under the MIT License.

๐Ÿ™ Credits

๐Ÿ”— Links

โญ Star this repo if you find it useful!

Made with โค๏ธ for the Laravel community