coolhax/bootstrap-laravel-crud-generator
Install the Package
composer require coolhax/bootstrap-laravel-crud-generator
Publish the config (if needed):
php artisan vendor:publish --provider="Cooltriks\CrudGenerator\CrudGeneratorServiceProvider"
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)./products).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
Access the CRUD
Visit /products in your browser. The package generates:
config/crud-generator.php (customize field types, view paths, or route prefixes).app/Models/Product.php (check $searchable property for filters).app/Http/Controllers/ProductController.php (extends Cooltriks\CrudGenerator\CrudController).resources/views/products/ (Bootstrap 5 templates with Blade components).resources/views/components/crud/.Generate a CRUD for User with a custom field (e.g., role):
php artisan crud:generate User name:string,email:string,role:string --fields
name, email, or role.php artisan crud:generate Post title:string,body:text,author_id:integer --migration
--migration: Creates a migration file.--seed: Generates a seeder with dummy data.--no-resource: Skips route registration (manual routes only).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
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.
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
User model from CRUD generation or manually register routes.--no-resource and define routes in routes/web.php:
Route::resource('products', ProductController::class)->except(['create', 'edit']);
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
Replace the default form logic with Laravel’s FormRequest:
php artisan make:request StoreProductRequest
public function store(StoreProductRequest $request)
{
// ...
}
Define $searchable in the model to enable filtering:
protected $searchable = ['name', 'description'];
The package automatically adds a search bar to the index view.
Route Conflicts
User CRUD vs. Laravel’s default auth).--no-resource and define routes manually. Prefix routes in config:
'route_prefix' => 'admin',
Missing Relationship Data
Category) or ensure the relationship is defined in the model.Bootstrap Asset Conflicts
bootstrap.css and bootstrap.js.npm install and npm run build after installing the package.Mass Assignment Risks
$fillable from the model, but forms may expose all fields.$fillable in the model or use $guarded:
protected $guarded = ['deleted_at'];
Pagination Limits
public function index()
{
return $this->crud->index(10); // Custom per-page count
}
Generator Errors
$fillable or $casts for typos. The generator validates these properties.--verbose for detailed output:
php artisan crud:generate Product --verbose
View Rendering Issues
php artisan view:clear
@extends('crud::layouts.app')).Relationship Dropdowns
Category has a posts() method if Product uses category_id).dd($this->crud->getRelationshipData()) in the controller’s create or edit methods.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/.
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.
Auto-Table Names The generator uses the model name (singular) for table names. Override in the model:
protected $table = 'my_custom_products';
Customizing the Generator Extend the generator class:
php artisan make:generator CustomCrudGenerator
Override methods like generateController() or generateView().
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"/>
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,
],
];
Localization Translate labels/
How can I help you explore Laravel packages today?