Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Crud Generator Laravel Package

ibex/crud-generator

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Steps to First Use

  1. Installation

    composer require ibex/crud-generator
    php artisan crud:install
    
    • This auto-installs Laravel Breeze (Tailwind) or Laravel UI (Bootstrap 5) if starting from a blank Laravel project.
  2. Generate a Basic CRUD

    php artisan crud:generate Post title:string,body:text --stack=tailwind
    
    • Generates:
      • Model (Post) with Eloquent relations
      • Controller (PostController) with index, create, store, etc.
      • Views (Tailwind CSS) in resources/views/posts/
      • Migration (if --migration flag is used)
  3. Where to Look First

    • Config File: config/crud-generator.php (customize stacks, default paths, or field mappings).
    • Generated Files: Check app/Http/Controllers/, app/Models/, and resources/views/ for auto-generated code.
    • Stack Options: --stack=bootstrap|tailwind|livewire|api (default: tailwind).
    • Field Types: Supports string, text, integer, boolean, date, datetime, and custom casts via --casts.

Implementation Patterns

Common Workflows

  1. Generating a CRUD with Relations

    php artisan crud:generate User name:string,email:string --with-relations --relation=hasMany:Post
    
    • Auto-generates:
      • User model with hasMany relation to Post.
      • Foreign key column in posts migration.
      • Relation dropdowns in the Livewire/Tailwind views.
  2. API-Only CRUD

    php artisan crud:generate Product name:string,price:decimal:2 --stack=api
    
    • Generates:
      • API resource controller with index, store, show, etc.
      • Form requests for validation (e.g., StoreProductRequest).
      • No views (ideal for SPAs or mobile apps).
  3. Livewire CRUD with Custom Logic

    php artisan crud:generate Task title:string,status:boolean --stack=livewire
    
    • Generates:
      • Livewire component (TaskComponent) with CRUD methods.
      • Tailwind-styled views with real-time updates.
    • Extend: Override methods in app/Http/Livewire/TaskComponent.php (e.g., custom validation or business logic).
  4. Reusing Generators for Similar Models

    • Use --fields to reuse field definitions across models:
      php artisan crud:generate Article title:string,body:text --fields=title:string,slug:string,published_at:datetime
      

Integration Tips

  • Custom Validation: Extend generated form requests (e.g., app/Http/Requests/StorePostRequest) to add rules.
  • Policy Integration: Attach policies to generated controllers:
    // app/Providers/AuthServiceProvider.php
    Gate::define('update-post', function ($user, $post) {
        return $user->can('edit', $post);
    });
    
  • Service Layer: Decouple business logic by injecting a service into the controller:
    public function __construct(private PostService $postService) {}
    
  • Testing: Use php artisan crud:test to generate feature tests for the CRUD (if supported in future versions).

Gotchas and Tips

Pitfalls

  1. Migration Conflicts

    • Running crud:generate without --migration skips migration creation. Manually create it later or use --migration to avoid inconsistencies.
    • Fix: Delete and regenerate the migration if columns are misaligned:
      php artisan crud:generate User name:string --migration --force
      
  2. Stack Mismatches

    • Livewire views are always generated in Tailwind CSS, even if --stack=bootstrap is used. This is intentional but may require manual adjustments.
    • Workaround: Override the Livewire component template in resources/views/livewire/.
  3. Relation Generation Quirks

    • Polymorphic relations (morphTo, morphMany) are not auto-detected. Define them manually in the model after generation.
    • Tip: Use --relation sparingly for complex relations; edit the model post-generation.
  4. Caching Issues

    • After updating the package, clear views and config caches:
      php artisan view:clear
      php artisan config:clear
      

Debugging

  • Generated Code Overwrites: The package regenerates files on subsequent runs with the same command. Use --force to overwrite or back up files first.
  • View Path Conflicts: If views are not found, check config/crud-generator.php for custom paths or run:
    php artisan crud:publish
    
    to publish assets.

Extension Points

  1. Custom Stacks

    • Extend the stack system by publishing and modifying the stack templates:
      php artisan vendor:publish --tag=crud-generator-stacks
      
    • Edit templates in resources/views/vendor/crud-generator/stacks/.
  2. Field Customization

    • Override field rendering by creating a custom view at resources/views/vendor/crud-generator/fields/{type}.blade.php.
    • Example: Add a custom rich_text field type.
  3. Post-Generation Hooks

    • Use service providers to run logic after CRUD generation:
      // app/Providers/AppServiceProvider.php
      public function boot()
      {
          CrudGenerator::generated(function ($modelName) {
              // Add custom logic, e.g., seed data or observers
          });
      }
      
  4. API Resource Extensions

    • Extend API responses by overriding the toArray() or toJson() methods in the generated model or resource class.

Pro Tips

  • Bulk Generation: Use a script to generate multiple CRUDs:
    php artisan crud:generate Post title:string,body:text --stack=tailwind
    php artisan crud:generate Comment body:text,post_id:integer --stack=tailwind --relation=belongsTo:Post
    
  • Dark Mode: For Tailwind, enable dark mode in the generated layout by adding dark: variants to classes.
  • Form Requests: Reuse validation logic across CRUDs by extending the generated form requests:
    class StorePostRequest extends CrudFormRequest {
        public function rules()
        {
            return array_merge(parent::rules(), [
                'custom_field' => 'required|unique:posts',
            ]);
        }
    }
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
comsave/common
alecsammon/php-raml-parser
chrome-php/wrench
lendable/composer-license-checker
typhoon/reflection
mesilov/moneyphp-percentage
mike42/gfx-php
bookdown/themes
aura/view
aura/html
aura/cli
povils/phpmnd
nayjest/manipulator
omnipay/tests
psr-mock/http-message-implementation
psr-mock/http-factory-implementation
psr-mock/http-client-implementation
voku/email-check
voku/urlify
rtheunissen/guzzle-log-middleware