adithwidhiantara / crud
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
Requires
- php: ^8.2
- illuminate/database: ^11.0|^12.0
- illuminate/http: ^11.0|^12.0
- illuminate/pagination: ^11.0|^12.0
- illuminate/routing: ^11.0|^12.0
- illuminate/support: ^11.0|^12.0
Requires (Dev)
- laravel/pint: dev-main
- orchestra/testbench: ^9.0
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.phpapp/Http/Controllers/ProductController.phpapp/Http/Services/ProductService.phpdatabase/migrations/xxxx_create_products_table.phpdatabase/factories/ProductFactory.phptests/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/productsPOST /api/productsGET /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()โnullablerule. - Default: Has
defaultvalue โ 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.
-
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.
-
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.