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

Bootstrap Laravel Crud Generator Laravel Package

coolhax/bootstrap-laravel-crud-generator

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Steps to First CRUD

  1. Install the Package

    composer require coolhax/bootstrap-laravel-crud-generator
    

    Publish the config (if needed):

    php artisan vendor:publish --provider="Cooltriks\CrudGenerator\CrudGeneratorServiceProvider"
    
  2. Generate a Model & CRUD Run the generator command for a new model (e.g., Product):

    php artisan crud:generate Product name:string,description:text,price:decimal,category_id:integer --fields
    
    • --fields: Manually define fields (optional; omit for auto-detection via migration).
    • Auto-routes: The package registers routes automatically (e.g., /products).
  3. Define Relationships (Optional) In your Product model, specify relationships (e.g., category_id):

    public function category()
    {
        return $this->belongsTo(Category::class);
    }
    

    Re-run the generator to include the dropdown in forms:

    php artisan crud:generate Product
    
  4. Access the CRUD Visit /products in your browser. The package generates:

    • Index (list with search/filter).
    • Create/Edit (forms with Bootstrap 5 and relationship dropdowns).
    • Show (detail view).

Where to Look First

  • Config File: config/crud-generator.php (customize field types, view paths, or route prefixes).
  • Generated Files:
    • Model: app/Models/Product.php (check $searchable property for filters).
    • Controller: app/Http/Controllers/ProductController.php (extends Cooltriks\CrudGenerator\CrudController).
    • Views: resources/views/products/ (Bootstrap 5 templates with Blade components).
  • Blade Components: Extend or override components in resources/views/components/crud/.

First Use Case: Quick Admin Panel

Generate a CRUD for User with a custom field (e.g., role):

php artisan crud:generate User name:string,email:string,role:string --fields
  • Result: A fully functional admin panel for managing users with search/filter by name, email, or role.

Implementation Patterns

Workflows

1. Standard CRUD Generation

php artisan crud:generate Post title:string,body:text,author_id:integer --migration
  • Flags:
    • --migration: Creates a migration file.
    • --seed: Generates a seeder with dummy data.
    • --no-resource: Skips route registration (manual routes only).

2. Extending Existing Models

Modify the model after generation (e.g., add a relationship):

// app/Models/Post.php
public function author()
{
    return $this->belongsTo(User::class);
}

Re-run the generator to update views:

php artisan crud:generate Post

3. Customizing Views

Override default views by copying them from vendor/cooltriks/bootstrap-laravel-crud-generator/resources/views to resources/views/products/. Example: Customize the form layout in resources/views/products/create.blade.php.

4. Dynamic Relationships

For polymorphic relationships (e.g., Comment on Post or Video), define the relationship in the model:

public function commentable()
{
    return $this->morphTo();
}

Generate the CRUD:

php artisan crud:generate Comment body:text,commentable_id:integer,commentable_type:string --fields

Integration Tips

Laravel Breeze/Jetstream

  • Conflict: The package auto-generates auth routes. If using Breeze/Jetstream, exclude the User model from CRUD generation or manually register routes.
  • Solution: Use --no-resource and define routes in routes/web.php:
    Route::resource('products', ProductController::class)->except(['create', 'edit']);
    

API Resources

Convert the CRUD to API-only by extending the controller:

// app/Http/Controllers/ProductController.php
public function __construct()
{
    $this->middleware('auth:sanctum')->except(['index', 'show']);
}

Use Resource controllers for API responses:

php artisan make:controller ProductApiController --api --resource

Form Requests

Replace the default form logic with Laravel’s FormRequest:

  1. Generate a request:
    php artisan make:request StoreProductRequest
    
  2. Update the controller to use it:
    public function store(StoreProductRequest $request)
    {
        // ...
    }
    

Searchable Fields

Define $searchable in the model to enable filtering:

protected $searchable = ['name', 'description'];

The package automatically adds a search bar to the index view.


Gotchas and Tips

Pitfalls

  1. Route Conflicts

    • Issue: Auto-routes may clash with existing routes (e.g., User CRUD vs. Laravel’s default auth).
    • Fix: Use --no-resource and define routes manually. Prefix routes in config:
      'route_prefix' => 'admin',
      
  2. Missing Relationship Data

    • Issue: Dropdowns in forms appear empty if related models aren’t generated.
    • Fix: Generate the related model first (e.g., Category) or ensure the relationship is defined in the model.
  3. Bootstrap Asset Conflicts

    • Issue: If using Laravel Mix/Vite, ensure Bootstrap 5 is properly compiled. The package expects bootstrap.css and bootstrap.js.
    • Fix: Run npm install and npm run build after installing the package.
  4. Mass Assignment Risks

    • Issue: Generated controllers use $fillable from the model, but forms may expose all fields.
    • Fix: Explicitly define $fillable in the model or use $guarded:
      protected $guarded = ['deleted_at'];
      
  5. Pagination Limits

    • Issue: Default pagination (15 items) may not suit all use cases.
    • Fix: Override the controller method:
      public function index()
      {
          return $this->crud->index(10); // Custom per-page count
      }
      

Debugging

  1. Generator Errors

    • Check the model’s $fillable or $casts for typos. The generator validates these properties.
    • Run with --verbose for detailed output:
      php artisan crud:generate Product --verbose
      
  2. View Rendering Issues

    • Clear compiled views:
      php artisan view:clear
      
    • Check for missing Blade components (e.g., @extends('crud::layouts.app')).
  3. Relationship Dropdowns

    • Ensure the inverse relationship exists (e.g., Category has a posts() method if Product uses category_id).
    • Debug with dd($this->crud->getRelationshipData()) in the controller’s create or edit methods.

Configuration Quirks

  1. Custom View Paths Override the default view path in config/crud-generator.php:

    'view_path' => 'custom-crud-views',
    

    Then generate views to resources/views/custom-crud-views/.

  2. Field Types The package supports custom field types via config:

    'field_types' => [
        'rich_text' => 'textarea',
        'custom_field' => 'input',
    ],
    

    Extend the generator’s FieldTypeResolver for complex types.

  3. Auto-Table Names The generator uses the model name (singular) for table names. Override in the model:

    protected $table = 'my_custom_products';
    

Extension Points

  1. Customizing the Generator Extend the generator class:

    php artisan make:generator CustomCrudGenerator
    

    Override methods like generateController() or generateView().

  2. Adding Custom Actions Extend the controller to add buttons (e.g., "Publish"):

    public function publish(Product $product)
    {
        $product->update(['is_published' => true]);
        return redirect()->route('products.edit', $product);
    }
    

    Add a button in the view:

    <x-crud.action :action="route('products.publish', $item)" label="Publish"/>
    
  3. Hooks for Post-Generation Use Laravel’s generated:crud event to run logic after generation:

    // app/Providers/EventServiceProvider.php
    protected $listen = [
        'generated:crud' => [
            \App\Listeners\PostCrudGeneration::class,
        ],
    ];
    
  4. Localization Translate labels/

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.
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
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle
atriumphp/atrium