rahulp/crud-generator

A Laravel package for generating CRUD operations with Repository Pattern

v1.0.3 2025-01-18 10:04 UTC

This package is auto-updated.

Last update: 2025-06-18 10:59:03 UTC


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