Installation:
composer require codebider/generate-crud
Publish the config file (if needed):
php artisan vendor:publish --provider="Codebider\GenerateCrud\GenerateCrudServiceProvider"
First Use Case:
Generate a basic CRUD for a Post model with fields:
php artisan crud:generate
Follow prompts to define:
Post).title:string, content:text, published:boolean).belongsTo:User).Where to Look First:
config/crud.php (customize paths, editor, or default fields).app/Models/, database/migrations/, app/Http/Controllers/, and resources/views/ for auto-generated files.php artisan crud:generate --help for CLI options.Interactive Workflow:
php artisan crud:generate
name:string|required, price:decimal:8,2).Non-Interactive Mode (CI/CD):
php artisan crud:generate Post --fields="title:string,content:text" --relationships="belongsTo:User"
Custom Templates:
resources/views/vendor/crud/.index.blade.php to add custom columns or buttons.Integration with Existing Code:
// app/Models/Post.php
public function getFormattedTitleAttribute()
{
return strtoupper($this->title);
}
app/Http/Controllers/PostController:
public function store(Request $request)
{
$request->merge(['slug' => Str::slug($request->title)]);
return parent::store($request);
}
routes/web.php:
Route::prefix('admin')->group(function () {
Route::resource('posts', PostController::class);
});
Relationship Handling:
hasMany:Comment).// app/Models/Comment.php
public function post()
{
return $this->belongsTo(Post::class);
}
Validation Rules:
rules() method in the controller:
protected function rules()
{
return [
'title' => 'required|string|max:255|unique:posts',
'content' => 'required|string|min:10',
];
}
API Resources:
API Resources:
php artisan make:resource PostResource
Then update the PostController to use the resource.Rapid Prototyping:
php artisan crud:generate Product --fields="name:string,price:decimal:8,2,stock:integer"
Team Collaboration:
php artisan crud:generate User --fields="email:string|email|unique,name:string" --relationships="hasMany:Post"
CRUD_SPEC.md file.Legacy System Integration:
php artisan crud:generate --table=legacy_users
Multi-Tenant Apps:
public function index()
{
$this->authorize('view', Tenant::class);
return parent::index()->where('tenant_id', auth()->tenant()->id);
}
Form Requests:
FormRequest for validation:
php artisan make:request StorePostRequest
public function store(StorePostRequest $request)
{
return parent::store($request);
}
Policy Integration:
php artisan make:policy PostPolicy --model=Post
public function __construct()
{
$this->authorizeResource(Post::class, 'post');
}
Testing:
public function test_crud_operations()
{
$response = $this->get('/posts');
$response->assertStatus(200);
$this->post('/posts', ['title' => 'Test', 'content' => 'Body'])
->assertRedirect();
}
Localization:
// resources/lang/en/crud.php
return [
'posts' => [
'title' => 'Blog Title',
'content' => 'Article Content',
],
];
<x-input label="{{ __('crud.posts.title') }}" />
File Uploads:
image:file).protected function store(Request $request)
{
$data = $request->except('image');
if ($request->hasFile('image')) {
$data['image_path'] = $request->file('image')->store('posts');
}
return parent::store($data);
}
Migration Conflicts:
--force to overwrite:
php artisan crud:generate --force
Field Name Collisions:
$fillable and $casts may conflict with custom methods. Rename generated methods:
// Avoid naming a method `price` if `price` is a field.
public function getPriceInDollarsAttribute()
{
return '$' . $this->price;
}
Relationship Sync Issues:
hasOne/belongsTo mismatches.View Overrides:
resources/views/vendor/crud/ must match the generator’s expected structure. Use php artisan crud:generate --dry-run to preview the expected files.Editor Integration:
--edit flag uses the editor defined in config/editor.php. Ensure your system’s EDITOR environment variable matches (e.g., export EDITOR="code --wait" for VSCode).Soft Deletes:
php artisan make:model Post -m --softDeletes
Then regenerate the CRUD or update the model/controller manually.Timestamps:
$dates property is updated to avoid created_at/updated_at errors.Dry Run:
php artisan crud:generate --dry-run
Log Output:
php artisan crud:generate --verbose
Manual Overrides:
touch app/Models/Post.php
php artisan crud:generate --skip-model
Database Errors:
decimal precision/scale).How can I help you explore Laravel packages today?