luthfi/simple-crud-generator
Installation
composer require luthfi/simple-crud-generator
Publish the config file:
php artisan vendor:publish --provider="Luthfi\SimpleCrudGenerator\SimpleCrudGeneratorServiceProvider"
Configuration
Edit config/simple-crud-generator.php to define:
First Use Case
Generate a CRUD for a Post model:
php artisan crud:generate Post
This creates:
app/Models/Post.php)database/migrations/..._create_posts_table.php)app/Http/Controllers/PostController.php)resources/views/posts/...)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)resources/views/overrides/posts/ (e.g., edit.blade.php).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']);
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',
];
php artisan crud:generate Post Category --fields="name,slug"
public function comments()
{
return $this->hasMany(Comment::class);
}
public function __construct(PostRepository $repository)
{
$this->repository = $repository;
}
Use the --api flag to generate API resources:
php artisan crud:generate Post --api
Generates:
PostResource (API response transformer).routes/api.php.Override the default Blade templates by publishing them:
php artisan vendor:publish --tag=simple-crud-views
Edit templates in resources/views/vendor/simple-crud/.
Use the --seed flag to generate a seeder:
php artisan crud:generate Post --seed
Edit the seeder in database/seeders/PostsTableSeeder.php.
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.
Migration Conflicts
--force to overwrite:
php artisan crud:generate Post --force
Namespace Collisions
App\Models), update config/simple-crud-generator.php:
'namespace' => [
'model' => 'App\Models',
'controller' => 'App\Http\Controllers',
],
Bootstrap 5 Assets Missing
bootstrap.css and bootstrap.js are linked in your layout file. The generator assumes Bootstrap 5 is already included.Route Conflicts
/posts vs. /admin/posts).--prefix flag:
php artisan crud:generate Post --prefix="admin"
Form Request Validation
FormRequest class manually and bind it in the controller:
public function store(StorePostRequest $request)
{
// ...
}
Check Generated Files
app/Http/Controllers/).--verbose:
php artisan crud:generate Post --verbose
Blade Template Errors
@extends or @section directives.vendor/luthfi/simple-crud-generator/resources/views.Database Errors
php artisan migrate
Controller Method Overrides
index()), ensure the parent method is called if needed:
public function index()
{
$posts = $this->repository->all();
return view('posts.index', compact('posts'));
}
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.
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'));
}
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);
});
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.
How can I help you explore Laravel packages today?