Generate Simple CRUD

Installs: 1

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 0

Forks: 0

Open Issues: 0

Type:package

pkg:composer/adithwidhiantara/crud

1.0.0 2025-12-30 07:19 UTC

This package is auto-updated.

Last update: 2025-12-30 07:39:27 UTC


README

Laravel CRUD Generator is a powerful package designed to accelerate backend development. It provides a full suite of features including Auto-Discovery Routes, Dynamic Database Schema Validation, Bulk Operations, and * Service-Repository Pattern* out of the box.

Stop writing repetitive boilerplate code. Just run one command and you are ready to go! ๐Ÿš€

โœจ Features

  • โšก Rapid Generation: Generate Model, Controller, Service, Factory, Migration, and Unit Test in one command.
  • ๐Ÿ›ฃ๏ธ Auto-Discovery Routes: No need to manually define routes in api.php. Routes are automatically registered based on your controller.
  • ๐Ÿ›ก๏ธ Dynamic Validation: Validation rules are automatically generated by inspecting your Database Schema (supports MySQL & PostgreSQL).
  • ๐Ÿ“ฆ Bulk Operations: Built-in support for Bulk Create, Bulk Update, and Bulk Delete in a single atomic transaction.
  • mj Service Pattern: Clean architecture separation using Service classes with built-in Hooks (beforeCreate, afterCreate, etc).
  • ๐Ÿงช Ready-to-use Tests: Automatically generates Feature Tests that cover standard CRUD scenarios.

๐Ÿ“ฆ Installation

Requires PHP 8.2+ and Laravel 11+.

composer require adithwidhiantara/crud

๐Ÿš€ Quick Start

1. Generate CRUD

Run the magic command to generate everything you need:

php artisan make:crud Product

This command will generate:

  • app/Models/Product.php
  • app/Http/Controllers/ProductController.php
  • app/Http/Services/ProductService.php
  • database/migrations/xxxx_create_products_table.php
  • database/factories/ProductFactory.php
  • tests/Feature/ProductControllerTest.php

2. Update Migration

Open the generated migration file and define your table schema:

Schema::create('products', function (Blueprint $table) {
    $table->id();
    $table->string('name'); // Auto-detected as 'required|string|max:255'
    $table->text('description')->nullable(); // Auto-detected as 'nullable|string'
    $table->integer('price')->default(0); // Auto-detected as 'integer' (optional input)
    $table->timestamps();
});

3. Migrate & Enjoy

Run the migration and your API is ready!

php artisan migrate

You can now access your API at:

  • GET /api/products
  • POST /api/products
  • GET /api/products/{id}
  • PUT /api/products/{id}
  • DELETE /api/products/{id}
  • POST /api/products/bulk

๐Ÿ“– Deep Dive

๐Ÿ›ก๏ธ Dynamic Validation

You don't need to write validation rules manually. The package inspects your database columns to generate rules:

  • Type: varchar(20) โ†’ string|max:20
  • Nullable: nullable() โ†’ nullable rule.
  • Default: Has default value โ†’ Field becomes optional.
  • PostgreSQL Support: Fully supports Postgres specific types and length constraints.

If you need custom rules, simply override the rules() method in your Controller's Request class (or creating a custom Request).

๐Ÿ“ฆ Bulk Operations

The /bulk endpoint allows you to perform multiple operations in a Single Database Transaction.

Endpoint: POST /api/products/bulk

Payload Example:

{
  "create": [
    {
      "name": "Laptop",
      "price": 15000000
    },
    {
      "name": "Mouse",
      "price": 200000
    }
  ],
  "update": {
    "10": {
      "price": 14500000
    },
    "12": {
      "name": "Gaming Mouse"
    }
  },
  "delete": [
    15,
    16,
    17
  ]
}

If any operation fails, the entire transaction is rolled back.

โš™๏ธ Model Configuration

Your models (extending CrudModel) have two powerful methods to control the API output and Validation behavior.

  1. getShowOnListColumns() This method is mandatory. It determines which columns are returned when calling the List Endpoint (GET /api/products). This helps optimize performance by selecting only necessary fields for table views.

  2. ignoredColumns() This method controls the Auto-Validation Generator. By default, the generator ignores system columns (id, created_at, updated_at, deleted_at).

If you have sensitive columns (like api_token, password_hash, is_admin) or calculated columns that should NOT be validated or accepted from the Request payload, override this method.

๐Ÿช Service Hooks

You can intervene in the CRUD process by overriding hooks in your Service class ( app/Http/Services/ProductService.php).

public function beforeCreateHook(array $data): array
{
    // Modify data before saving to DB
    $data['slug'] = Str::slug($data['name']);
    $data['user_id'] = auth()->id();
    
    return $data;
}

public function afterCreateHook(CrudModel $model): CrudModel
{
    // Trigger actions after save (e.g., Send Email)
    Mail::to(auth()->user())->send(new ProductCreated($model));
    
    return $model;
}

๐Ÿ›ฃ๏ธ Custom Endpoints

To add a custom route to your controller that is automatically discovered, use the #[Endpoint] attribute.

use Adithwidhiantara\Crud\Attributes\Endpoint;

class ProductController extends BaseCrudController
{
    #[Endpoint(method: 'POST', uri: 'publish/{id}')]
    public function publish($id)
    {
        $this->service()->publish($id);
        return response()->json(['message' => 'Published!']);
    }
}

Result: POST /api/products/publish/{id}

โœ… Testing

The package automatically generates feature tests for your CRUD. To run them:

php artisan test

Ensure you have set up your phpunit.xml or .env.testing database configuration.

License

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