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

Crudgen Laravel Package

mrdebug/crudgen

View on GitHub
Deep Wiki
Context7

Getting Started

First Steps

  1. Installation: Run composer require mrdebug/crudgen --dev and publish assets with:

    php artisan vendor:publish --provider="Mrdebug\Crudgen\CrudgenServiceProvider"
    

    This sets up config, stubs, and default views.

  2. Quick CRUD Generation: Generate a basic CRUD for a Post model with:

    php artisan crudgen:create Post --fields="title:string,content:text" --hasMany=Comment --belongsToMany=Tag
    

    This auto-creates:

    • Controller (PostController)
    • Model (Post.php) with relationships
    • Migration (posts_table)
    • Request validation (StorePostRequest, UpdatePostRequest)
    • Views (Blade templates for index, create, edit, show)
    • API routes (if --api flag is used)
  3. First Use Case: Test the CRUD by visiting /posts (web) or /api/posts (API). The package handles:

    • Form validation (via Laravel Collective HTML)
    • Relationship seeding (e.g., Comment, Tag models are stubbed)
    • Basic CRUD operations (index, store, update, destroy).

Implementation Patterns

Core Workflows

  1. Model-Driven Generation:

    • Define fields and relationships once in the CLI command. Example:
      php artisan crudgen:create User --fields="name:string,email:string:unique" --belongsTo=Role --morphMany=Activity
      
    • The package infers:
      • Migration columns (with nullable defaults).
      • Model relationships (belongsTo, hasMany, morphMany).
      • Request validation rules (e.g., required|email for email).
  2. View Customization:

    • Views are stored in resources/views/vendor/crudgen/{model}/.
    • Override defaults by copying the template to resources/views/{model}/ and modifying.
    • Use @include('crudgen::partials.form-field', ['field' => 'content']) for reusable field rendering.
  3. API Integration:

    • Generate a REST API with --api:
      php artisan crudgen:create Product --fields="name:string,price:decimal:8,2" --api
      
    • Auto-routes: GET /api/products, POST /api/products, etc.
    • API responses include relationships (eager-loaded via with()).
  4. Relationship Handling:

    • HasMany/BelongsTo: Auto-generates nested routes (e.g., /posts/{post}/comments).
    • BelongsToMany: Creates a pivot table migration and includes a checkbox field in forms.
    • Polymorphic: Supports morphTo/morphMany (e.g., for activity logs).
  5. Validation and Authorization:

    • Request classes (Store{Model}Request, Update{Model}Request) include default rules.
    • Extend with custom logic in app/Http/Requests:
      public function rules()
      {
          return array_merge(parent::rules(), ['custom_field' => 'required']);
      }
      
  6. Seeding:

    • Use --seed to generate a seeder for the model:
      php artisan crudgen:create Category --fields="name:string" --seed
      
    • Seeder populates the table with sample data (configurable via config/crudgen.php).

Integration Tips

  • Existing Projects:
    • Run php artisan crudgen:install to set up config and views after installation.
    • For partial CRUDs, generate only the model/migration first, then manually create the controller.
  • Custom Fields:
    • Add custom field types via config/crudgen.php under custom_fields:
      'custom_fields' => [
          'rich_text' => ['type' => 'textarea', 'attributes' => ['rows' => 5]],
      ],
      
  • Localization:
    • Translate labels/buttons by publishing language files:
      php artisan vendor:publish --tag=crudgen-lang
      
    • Override in resources/lang/{locale}/crudgen.php.
  • Testing:
    • Use CrudgenTestCase (included) to test generated CRUDs:
      use Mrdebug\Crudgen\Testing\CrudgenTestCase;
      
      class PostCrudTest extends CrudgenTestCase
      {
          public function testCreatePost()
          {
              $this->createCrud('Post');
              $this->post('/posts', ['title' => 'Test', 'content' => 'Body']);
              $this->assertDatabaseHas('posts', ['title' => 'Test']);
          }
      }
      

Gotchas and Tips

Pitfalls and Debugging

  1. Migration Conflicts:

    • If the table already exists, delete the migration file or use --force:
      php artisan crudgen:create Post --fields="title:string" --force
      
    • Tip: Use --no-migration to skip migration creation if needed.
  2. Relationship Errors:

    • Ensure related models exist before generating CRUDs. Example:
      php artisan crudgen:create Comment --fields="content:text" --belongsTo=Post
      
      Run this after generating the Post CRUD.
    • Debug: Check config/crudgen.php for relationships validation.
  3. View Overrides:

    • If views aren’t updating, clear the view cache:
      php artisan view:clear
      
    • Tip: Use @extends('crudgen::layouts.app') in custom views to inherit the base layout.
  4. API Route Conflicts:

    • API routes are prefixed with api/. If conflicts arise, rename the model or adjust routes in routes/api.php.
    • Debug: Run php artisan route:list to check for duplicates.
  5. Validation Rule Mismatches:

    • Custom validation rules in Update{Model}Request may conflict with auto-generated ones.
    • Fix: Extend the request class and merge rules:
      public function rules()
      {
          return array_merge(parent::rules(), ['price' => 'min:0']);
      }
      
  6. Polymorphic Relationships:

    • Ensure the morphs table exists (e.g., activities). The package auto-creates it for morphMany.
    • Gotcha: Polymorphic models must have a morphMap in AppServiceProvider:
      public function boot()
      {
          \Mrdebug\Crudgen\Crudgen::morphMap([
              'post' => \App\Models\Post::class,
              'comment' => \App\Models\Comment::class,
          ]);
      }
      
  7. Configuration Overrides:

    • Default config values are in config/crudgen.php. Common overrides:
      'default_namespace' => 'App\\Http\\Controllers\\Admin',
      'use_api_resources' => true,
      'form_request_namespace' => 'App\\Http\\Requests\\Admin',
      

Extension Points

  1. Custom Field Types:

    • Add new field types by extending Mrdebug\Crudgen\Field\FieldInterface:
      namespace App\Crudgen\Fields;
      
      use Mrdebug\Crudgen\Field\FieldInterface;
      
      class RichTextField implements FieldInterface
      {
          public function render()
          {
              return '<trix-editor>' . $this->value . '</trix-editor>';
          }
      }
      
    • Register in config/crudgen.php:
      'custom_fields' => [
          'rich_text' => \App\Crudgen\Fields\RichTextField::class,
      ],
      
  2. Custom Actions:

    • Add custom controller methods by extending the generated controller:
      namespace App\Http\Controllers;
      
      use Mrdebug\Crudgen\CrudController;
      
      class PostController extends CrudController
      {
          public function publish($id)
          {
              $post = $this->model->findOrFail($id);
              $post->update(['status' => 'published']);
              return back()->with('success', 'Post published!');
          }
      }
      
    • Add a route in routes/web.php:
      Route::put('/posts/{id}/publish', [PostController::class, 'publish'])->name('posts.publish');
      
  3. Custom Views:

    • Override the entire view by publishing the template:
      php artisan vendor:publish --tag=crudgen-views
      
    • Modify resources/views/vendor/crudgen/{model}/index.blade.php.
  4. Custom API Resources:

    • Replace the auto-generated API resource by defining a custom one:
      namespace App\Http\Resources;
      
      use Mrdebug\Crudgen\CrudResource;
      
      class PostResource extends CrudResource
      
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.
emuniq/filament-browser-notifications
syriable/filament-translator
hungnm28/livewire-form
wenprise/eloquent
crudly/encrypted
fadion/bouncy
cuci/prototurk-sdk
gos/pubsub-router-bundle
cuci/prototurk-sdk-symfony
clementtalleu/easyadmin-markdown-bundle
codeflextech/permission-manager
karnoweb/livewire-datepicker
sayedenam/sayed-dashboard
milito/query-filter
apiboxsym/user-bundle
apiboxsym/health-check-bundle
jayeshmepani/jpl-moshier-ephemeris-php
elnasnato/laraliveui
labrodev/rest-sdk
sampaui/sampaui