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

Simple Crud Generator Laravel Package

luthfi/simple-crud-generator

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation

    composer require luthfi/simple-crud-generator
    

    Publish the config file:

    php artisan vendor:publish --provider="Luthfi\SimpleCrudGenerator\SimpleCrudGeneratorServiceProvider"
    
  2. Configuration Edit config/simple-crud-generator.php to define:

    • Default namespace for generated models/controllers.
    • Bootstrap version (5 by default).
    • Custom blade templates (if needed).
  3. First Use Case Generate a CRUD for a Post model:

    php artisan crud:generate Post
    

    This creates:

    • Model (app/Models/Post.php)
    • Migration (database/migrations/..._create_posts_table.php)
    • Controller (app/Http/Controllers/PostController.php)
    • Blade views (resources/views/posts/...)
    • Routes (routes/web.php)

    Key Files to Check First:

    • config/simple-crud-generator.php (customization)
    • app/Http/Controllers/PostController.php (logic hooks)
    • resources/views/posts/index.blade.php (UI tweaks)

Implementation Patterns

1. Customizing Generated CRUD

A. Overriding Views

  • Place custom views in resources/views/overrides/posts/ (e.g., edit.blade.php).
  • The generator will use these instead of defaults.

B. Extending Controllers

Add methods to the generated controller (e.g., PostController.php):

public function customAction()
{
    return response()->json(['custom' => 'data']);
}

Register the route in routes/web.php:

Route::get('/posts/custom', [PostController::class, 'customAction']);

C. Field-Specific Logic

Use fillable and rules in the model to auto-generate form fields:

protected $fillable = ['title', 'content', 'published_at'];
protected $rules = [
    'title' => 'required|max:255',
    'content' => 'required',
];

2. Workflows

A. Rapid Prototyping

  • Generate CRUD for multiple models in one command:
    php artisan crud:generate Post Category --fields="name,slug"
    
  • Fields are auto-detected from the migration or model.

B. Integration with Existing Code

  • Models: Extend the generated model to add relationships:
    public function comments()
    {
        return $this->hasMany(Comment::class);
    }
    
  • Controllers: Inject dependencies (e.g., repositories):
    public function __construct(PostRepository $repository)
    {
        $this->repository = $repository;
    }
    

C. API-Only CRUD

Use the --api flag to generate API resources:

php artisan crud:generate Post --api

Generates:

  • PostResource (API response transformer).
  • API routes in routes/api.php.

3. Advanced Features

A. Custom Templates

Override the default Blade templates by publishing them:

php artisan vendor:publish --tag=simple-crud-views

Edit templates in resources/views/vendor/simple-crud/.

B. Seeding Data

Use the --seed flag to generate a seeder:

php artisan crud:generate Post --seed

Edit the seeder in database/seeders/PostsTableSeeder.php.

C. Soft Deletes

Enable soft deletes in the model:

use Illuminate\Database\Eloquent\SoftDeletes;
use HasSoftDeletes;

class Post extends Model
{
    use SoftDeletes;
    protected $dates = ['deleted_at'];
}

The generator will include soft-delete logic in the controller.


Gotchas and Tips

Pitfalls

  1. Migration Conflicts

    • If the table already exists, the generator skips migration creation. Manually adjust or drop the table first.
    • Fix: Use --force to overwrite:
      php artisan crud:generate Post --force
      
  2. Namespace Collisions

    • If your app uses a custom namespace (e.g., App\Models), update config/simple-crud-generator.php:
      'namespace' => [
          'model' => 'App\Models',
          'controller' => 'App\Http\Controllers',
      ],
      
  3. Bootstrap 5 Assets Missing

    • Ensure bootstrap.css and bootstrap.js are linked in your layout file. The generator assumes Bootstrap 5 is already included.
  4. Route Conflicts

    • Generated routes may clash with existing ones (e.g., /posts vs. /admin/posts).
    • Fix: Use the --prefix flag:
      php artisan crud:generate Post --prefix="admin"
      
  5. Form Request Validation

    • The generator creates basic validation in the controller. For complex rules, create a FormRequest class manually and bind it in the controller:
      public function store(StorePostRequest $request)
      {
          // ...
      }
      

Debugging Tips

  1. Check Generated Files

    • Verify files are created in the correct locations (e.g., app/Http/Controllers/).
    • Debug: Run with --verbose:
      php artisan crud:generate Post --verbose
      
  2. Blade Template Errors

    • If views break, check for missing @extends or @section directives.
    • Fix: Compare with the default templates in vendor/luthfi/simple-crud-generator/resources/views.
  3. Database Errors

    • Ensure the migration runs before accessing the CRUD:
      php artisan migrate
      
  4. Controller Method Overrides

    • If you override a method (e.g., index()), ensure the parent method is called if needed:
      public function index()
      {
          $posts = $this->repository->all();
          return view('posts.index', compact('posts'));
      }
      

Extension Points

  1. Add Custom Fields Dynamically Extend the generator’s field detection by publishing the field resolver:

    php artisan vendor:publish --tag=simple-crud-fields
    

    Edit app/Providers/SimpleCrudGeneratorServiceProvider.php to add custom logic.

  2. Hooks for Post-Generation Use Laravel’s generated:crud event to run tasks after generation:

    // In EventServiceProvider
    public function boot()
    {
        event(new \Luthfi\SimpleCrudGenerator\Events\CrudGenerated('Post'));
    }
    
  3. Multi-Auth Support For admin/tenant separation, override the route prefix:

    // In routes/web.php
    Route::prefix('admin')->group(function () {
        Route::resource('posts', PostController::class);
    });
    
  4. Localization The generator supports localization via Blade’s @lang directives. Extend the views to include:

    <h1>{{ __('posts.title') }}</h1>
    

    Add translations in resources/lang/en/posts.php.

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.
milito/query-filter
apiboxsym/user-bundle
apiboxsym/health-check-bundle
jayeshmepani/jpl-moshier-ephemeris-php
elnasnato/laraliveui
labrodev/rest-sdk
sampaui/sampaui
babelqueue/php-sdk
facebook/capi-param-builder-php
babelqueue/symfony
hamzi/corewatch
minionfactory/raw-hydrator
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours