PHPackages                             mohammedhassan/crud-package - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Framework](/categories/framework)
4. /
5. mohammedhassan/crud-package

ActiveLaravel-package[Framework](/categories/framework)

mohammedhassan/crud-package
===========================

A Laravel package to generate CRUD scaffolds.

v1.0.4(10mo ago)1931MITPHPPHP ^8.1

Since Jul 7Pushed 10mo agoCompare

[ Source](https://github.com/MohammedAbdo2344/CRUD-Package)[ Packagist](https://packagist.org/packages/mohammedhassan/crud-package)[ RSS](/packages/mohammedhassan-crud-package/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (12)Versions (6)Used By (0)

Laravel CRUD Generator Command
==============================

[](#laravel-crud-generator-command)

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.

Features
--------

[](#features)

- Generates a full CRUD setup with a single command.
- Uses Data Transfer Objects (DTOs) for validation and data handling.
- Creates a dedicated Service layer for business logic.
- Automatically updates the migration file based on a schema definition.
- Adds API resource routes automatically.
- Highly customizable through options.

Installation
------------

[](#installation)

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.

Usage
-----

[](#usage)

To generate a new CRUD module, use the following Artisan command:

```
php artisan make:crud {name} {--schema=} {--api-route=} {--controller-route=}
```

### Arguments

[](#arguments)

- `{name}`: The name of the resource (e.g., `Product`). This will be used to generate the class names.

### Options

[](#options)

- `--schema=`: (Optional) The path to a schema file. The schema file defines the validation rules and database columns for the model.
- `--api-route=`: (Optional) The path to the API routes file where the resource route will be added. Defaults to `routes/api.php`.
- `--controller-route=`: (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`.

### Basic Example

[](#basic-example)

This command will generate the CRUD files for a "Product" resource.

```
php artisan make:crud Product
```

### Advanced Example with Schema

[](#advanced-example-with-schema)

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
```

What It Generates
-----------------

[](#what-it-generates)

The `make:crud` command creates the following files and components:

1. **Model**: `app/Models/{Name}.php`

    - Includes standard Eloquent setup.
    - Adds helper methods for CRUD operations (`store{Name}`, `update{Name}`, etc.).
2. **Migration**: `database/migrations/..._create_{name_plural}_table.php`

    - Creates the database table for the model.
    - If a `--schema` is provided, it automatically adds the columns to the migration file.
3. **Controller**: `app/Http/Controllers/{Name}Controller.php`

    - A standard RESTful controller with `index`, `store`, `update`, and `destroy` methods.
    - Uses the generated Service and DTOs for handling requests.
4. **Service**: `app/Services/{Name}Service.php`

    - Contains the business logic for the CRUD operations, keeping the controller thin.
5. **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.
6. **API Resource**: `app/Http/Resources/{Name}Resource.php`

    - For transforming the model into a JSON response.
7. **Route**:

    - Appends an `apiResource` route to your API routes file (e.g., `routes/api.php`).
8. **Response Helper**: `app/Helpers/ResponsesHelper.php`

    - A helper class for generating standardized JSON API responses. This is only created if it doesn't already exist.

The Schema File
---------------

[](#the-schema-file)

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`**

```
