- How do I generate a Laravel API CRUD for a model with foreign key constraints?
- Use the `make:crud` command with `--fields` specifying the foreign key and constraints. For example: `php artisan make:crud Department --fields="name:string|max:255;created_by:foreign|constrained:users|onDelete:cascade"`. This generates a model, migration, controller, and API resource with the foreign key relationship and cascade delete logic.
- Can I exclude certain files (e.g., Blade views) when generating a CRUD?
- Yes, use the `--exclude` flag to skip specific files. For example, `php artisan make:crud Article --fields="title:string" --exclude=views` will generate everything except Blade views. You can exclude multiple files by comma-separating them (e.g., `--exclude=model,views`).
- Does this package work with Laravel 10 and newer versions?
- Yes, `mr.incognito/crudify` is designed for modern Laravel versions (9.x and 10.x). It leverages Laravel’s core components like Eloquent, Migrations, and API Resources, ensuring compatibility. Always check the package’s GitHub repo for the latest Laravel version support before installing.
- How do I add default values or nullable fields to a generated CRUD?
- Use the `--fields` syntax with `~` for nullable fields and `|default:value` for defaults. For example: `php artisan make:crud Product --fields="name:string~|default:Unnamed;price:decimal|default:0.00"`. This will generate nullable `name` and a `price` field with a default value of `0.00`.
- What if I need to customize the generated controller or request logic?
- After generation, manually edit the generated files in `app/Http/Controllers/` or `app/Http/Requests/`. The package provides sensible defaults, but you can override methods like `store()`, `update()`, or validation rules in the `FormRequest` class. Use `--exclude=controller` if you prefer to write your own from scratch.
- Is there a way to clean up or remove all generated CRUD files?
- Yes, the package includes a `delete:crud` command. Run `php artisan delete:crud ModelName` (e.g., `php artisan delete:crud Department`) to remove all files generated for that CRUD, including models, migrations, controllers, and views. This is useful for testing or resetting generated code.
- Will this package work with non-Blade frontend frameworks like Livewire or Inertia.js?
- For API CRUDs (`--type=api`), it works seamlessly with Livewire, Inertia.js, or Vue/React. For web CRUDs (`--type=web`), Blade views are generated by default, which may not integrate directly. Use `--exclude=views` and manually link to your custom frontend templates or components.
- How do I generate a CRUD for an existing table with data?
- Avoid generating migrations for existing tables to prevent conflicts. Use `--exclude=migration` and manually create the migration if needed. The package generates models, controllers, and requests safely, but you’ll need to handle schema changes (e.g., adding columns) separately.
- Are the generated validation rules type-safe and customizable?
- Yes, the package generates type-aware validation rules (e.g., `string|max:255`, `integer|min:0`) based on your `--fields` input. To customize, extend the generated `FormRequest` class (e.g., add `unique:except,$id` rules) or override validation logic in the controller. The rules align with Laravel’s validation system.
- What are some alternatives to mr.incognito/crudify for Laravel CRUD generation?
- Alternatives include `laravel-shift/crud-generator` (more opinionated), `spatie/laravel-model-generator` (simpler, model-focused), and `orchid/software` (admin panel with CRUD). Unlike these, `mr.incognito/crudify` emphasizes modularity (exclude files), dual API/web support, and cleanup commands. Choose based on whether you need Blade views, API-first, or admin panel integration.