This package provides an Artisan command make:crud to quickly scaffold a complete CRUD (Create, Read, Update, Delete) module for your Laravel application. It generates a Model, Migration, Controller, Service, DTOs, and an API Resource, following a structured and scalable pattern.
You can install the package via composer:
composer require mohammedhassan/crud-package
The package will automatically register its service provider. The necessary dependency wendelladriel/laravel-validated-dto will be installed automatically.
To generate a new CRUD module, use the following Artisan command:
php artisan make:crud {name} {--schema=} {--api-route=} {--controller-route=}
{name}: The name of the resource (e.g., Product). This will be used to generate the class names.--schema=<path>: (Optional) The path to a schema file. The schema file defines the validation rules and database columns for the model.--api-route=<path>: (Optional) The path to the API routes file where the resource route will be added. Defaults to routes/api.php.--controller-route=<path>: (Optional) A sub-path for the controller's namespace and directory. For example, V1/Admin will place the controller in App/Http/Controllers/V1/Admin.This command will generate the CRUD files for a "Product" resource.
php artisan make:crud Product
This command will generate the CRUD files for a "Post" resource, using a schema definition for validation and migration fields.
php artisan make:crud Post --schema=app/Schemas/PostSchema.php
The make:crud command creates the following files and components:
Model: app/Models/{Name}.php
store{Name}, update{Name}, etc.).Migration: database/migrations/..._create_{name_plural}_table.php
--schema is provided, it automatically adds the columns to the migration file.Controller: app/Http/Controllers/{Name}Controller.php
index, store, update, and destroy methods.Service: app/Services/{Name}Service.php
DTOs (Data Transfer Objects): app/DTOs/Service/{Name}/
Store{Name}DTO.php: For validating store requests.Update{Name}DTO.php: For validating update requests.Delete{Name}DTO.php: For validating delete requests.List{Name}DTO.php: For handling listing/pagination parameters.API Resource: app/Http/Resources/{Name}Resource.php
Route:
apiResource route to your API routes file (e.g., routes/api.php).Response Helper: app/Helpers/ResponsesHelper.php
The schema file allows you to define the fields for your model. It should be a PHP file that returns an array where keys are the field names and values are their Laravel validation rules. The command uses these rules to generate DTOs and migration columns.
Example Schema: app/Schemas/PostSchema.php
<?php
// app/Schemas/PostSchema.php
return [
'Post' => [
'title' => 'required|string|max:255',
'body' => 'required|string',
'is_published' => 'sometimes|boolean',
'user_id' => 'required|integer|exists:users,id',
],
// You can define other model schemas here
];
The command will look for the key that matches the model name (e.g., Post).
--controller-route option to organize controllers into subdirectories. For example, --controller-route=Api/V1 will create the controller at app/Http/Controllers/Api/V1/{Name}Controller.php.--api-route option to specify a different file for adding the API routes, like routes/admin.php.This CRUD generator was created by Mohammed Hassan.
How can I help you explore Laravel packages today?