fariddomat/auto-crud

Auto Crud For laravel applications

dev-main 2025-03-04 15:31 UTC

This package is auto-updated.

Last update: 2025-06-04 15:58:31 UTC


README

Auto-Crud is a Laravel package that automates the generation of complete CRUD (Create, Read, Update, Delete) functionality for your models. It streamlines development by creating models, controllers, migrations, views, and routes with a single interactive command. The package supports customizable fields (e.g., strings, decimals, selects, booleans, files, images), optional API controllers, dashboard prefixes, soft deletes, and middleware, all with a colorful command-line experience.

Installation

    composer require fariddomat/auto-crud:dev-main

Publish the Blade views for CRUD operations:

    php artisan vendor:publish --provider="Fariddomat\AutoCrud\AutoCrudServiceProvider" --tag="autocrud-views"

This copies the customizable Blade views (create, edit, index) to your resources/views/vendor/auto-crud directory.

Usage

Create a CRUD Module

Use the interactive make:auto-crud Artisan command to generate a CRUD module. The command prompts you for details step-by-step, with hints and colored output:

bashphp artisan make:auto-crud

Interactive Example

text Welcome to AutoCRUD Generator! Let's create your CRUD module step-by-step. What is the name of your model? (e.g., Post, User; must start with a capital letter): Product Let's define the fields for Product. Format: name:type:modifiers (e.g., title:string:nullable, user_id:select). Leave blank to finish. Enter a field (or press Enter to skip): name:string Enter a field (or press Enter to skip): price:decimal Enter a field (or press Enter to skip): category_id:select Enter a field (or press Enter to skip): <Enter> Generate an API controller instead of a web controller? (Default: No) [N/y]: y Enable soft deletes for Product? (Default: No) [N/y]: y Force overwrite existing files if they exist? (Default: No) [N/y]: n Enter middleware (comma-separated, e.g., auth,admin) or leave blank for none: auth Generating CRUD with the following settings: Model: Product Fields: name:string, price:decimal, category_id:select Type: API Dashboard: No Soft Deletes: Yes Force Overwrite: No Middleware: auth Proceed with these settings? [Y/n]: <Enter> Generating Auto CRUD for Product...

This generates:

  • Model: Product with soft deletes and fillable fields (name, price, category_id).
  • Controller: ProductApiController with API methods (index, create, store, show, edit, update, destroy, restore).
  • Migration: Table products with columns id, name, price, category_id, deleted_at, created_at, updated_at.
  • Routes: API routes in routes/api.php with auth middleware.

Web Example with Dashboard

text Welcome to AutoCRUD Generator! Let's create your CRUD module step-by-step. What is the name of your model? (e.g., Post, User; must start with a capital letter): Item Let's define the fields for Item. Format: name:type:modifiers (e.g., title:string:nullable, user_id:select). Leave blank to finish. Enter a field (or press Enter to skip): name:string Enter a field (or press Enter to skip): description:text Enter a field (or press Enter to skip): image:image Enter a field (or press Enter to skip): <Enter> Generate an API controller instead of a web controller? (Default: No) [N/y]: n Place the CRUD under a dashboard prefix? (Default: No) [N/y]: y Enable soft deletes for Item? (Default: No) [N/y]: n Force overwrite existing files if they exist? (Default: No) [N/y]: y Enter middleware (comma-separated, e.g., auth,admin) or leave blank for none: auth,admin Proceed with these settings? [Y/n]: <Enter>

This generates:

  • Model: Item with fillable fields (name, description, image).
  • Controller: ItemController in App\Http\Controllers\Dashboard with web methods.
  • Migration: Table items with columns id, name, description, image, created_at, updated_at.
  • Views: resources/views/dashboard/items/create.blade.php, edit.blade.php, index.blade.php.
  • Routes: Web routes in routes/web.php under dashboard prefix with auth and admin middleware.

Fields

Define fields interactively with the following supported types:

Field TypeDescriptionExample Usage
stringSimple text inputname:string
decimalDecimal number (e.g., prices)price:decimal
integerInteger numberquantity:integer
textLonger text fielddescription:text
selectDropdown with relational optionsuser_id:select
booleanCheckbox (true/false)is_active:boolean
fileSingle file upload (e.g., PDF)manual:file
imageSingle image uploadthumbnail:image
imagesMultiple image uploadsgallery:images

Modifiers like :nullable or :unique can be appended (e.g., name:string:nullable).

Features

Interactive CLI: Generate CRUD with a colorful, step-by-step command-line interface.
Automatic CRUD Generation: Creates models, controllers, migrations, views, and routes in one go.
Customizable Fields: Supports a variety of data types, including relations and file uploads.
API Support: Generates RESTful API controllers with JSON responses (e.g., 404 for missing records).
Dashboard Support: Prefixes routes with dashboard for admin panels.
Soft Deletes: Optional soft delete functionality with restore support.
Middleware: Apply custom middleware (e.g., auth, admin) to routes and controllers.
Blade Views: Dynamic create, edit, and index views with form fields matching your model.
File & Image Uploads: Handles single/multiple uploads with ImageHelper or Laravel’s storage system.

File & Image Handling

Single Image Upload

For image fields, the controller uses ImageHelper (if available) or Laravel’s storage:

phpif ($request->hasFile('thumbnail') && class_exists('App\Helpers\ImageHelper')) { $validated['thumbnail'] = ImageHelper::storeImageInPublicDirectory($request->file('thumbnail'), 'uploads/items'); } elseif ($request->hasFile('thumbnail')) { $validated['thumbnail'] = $request->file('thumbnail')->store('uploads/items', 'public'); }

Multiple Image Uploads

For images fields, processes multiple files into a JSON-encoded array:

phpif ($request->hasFile('gallery') && class_exists('App\Helpers\ImageHelper')) { $validated['gallery'] = []; foreach ($request->file('gallery') as $image) { $validated['gallery'][] = ImageHelper::storeImageInPublicDirectory($image, 'uploads/items'); } $validated['gallery'] = json_encode($validated['gallery']); } elseif ($request->hasFile('gallery')) { $validated['gallery'] = json_encode(array_map(fn($file) => $file->store('uploads/items', 'public'), $request->file('gallery'))); }

File Upload

For file fields, stores using Laravel’s storage:

phpif ($request->hasFile('manual')) { $validated['manual'] = $request->file('manual')->store('uploads/manuals', 'public'); }

Configuration

  • Customize Views: Edit the generated Blade files in resources/views/[dashboard/]model_name/ or the published views in resources/views/vendor/auto-crud.
  • Adjust Controllers: Modify the generated controllers in app/Http/Controllers/ or app/Http/Controllers/Dashboard/.
  • Tweak Migrations: Update the migration files in database/migrations/ as needed.

Contribution

Contributions are welcome! Submit issues, bug fixes, or pull requests via GitHub to help improve Auto-Crud.

License

Auto-Crud is licensed under the MIT License. See the LICENSE file for details.

Changes Made

  1. Interactive CLI: Updated the usage section to reflect the new interactive make:auto-crud command with colored output.
  2. Feature Enhancements: Added soft deletes, middleware, and JSON error handling for API controllers.
  3. Field Updates: Included integer in the field table and clarified modifier support.
  4. Examples: Rewritten to show the interactive flow instead of static commands.
  5. Consistency: Ensured terminology and examples match the latest package state (e.g., ApiController suffix for API).