jcsoriano / laravel-crud-templates
Laravel CRUD Templates - Generate complete CRUD operations with a single command
Fund package maintenance!
JC Soriano
Installs: 1
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 0
Forks: 0
Open Issues: 4
pkg:composer/jcsoriano/laravel-crud-templates
Requires
- php: ^8.4
- binafy/laravel-stub: ^1.1
- illuminate/contracts: ^11.0||^12.0
- spatie/laravel-package-tools: ^1.16
Requires (Dev)
- larastan/larastan: ^3.0
- laravel/pint: ^1.14
- nunomaduro/collision: ^8.8
- orchestra/testbench: ^10.0.0||^9.0.0
- pestphp/pest: ^4.0
- pestphp/pest-plugin-arch: ^4.0
- pestphp/pest-plugin-laravel: ^4.0
- phpstan/extension-installer: ^1.4
- phpstan/phpstan-deprecation-rules: ^2.0
- phpstan/phpstan-phpunit: ^2.0
This package is auto-updated.
Last update: 2025-11-16 10:29:13 UTC
README
CRUD Templates for Laravel allows you to generate controllers, models, policies, requests, resources, migrations, factories, and even tests - all the files you need to complete CRUD features with a single command. You can completely modify the template or create your own templates to fit your project's conventions perfectly.
FAQs
- Why do I need this if AI can generate the files for me? AI is nondeterministic. Sometimes, it won't follow your conventions perfectly, sometimes it may miss things. This generates all of the files for you programmatically - it will be complete and exact, and a great base for further work with your AI.
- What if the generated files don't fit my project's conventions? Everything is customizable. You can modify the stubs, add more file types to generate, or even create your own CRUD templates - create multiple for different scenarios!
- Will this do everything for me? The aim of this package is to generate almost everything for you. For simple CRUD features you can already get away with what it generates without modification. For features requiring a little bit of custom logic, this package will generate the first 90% of the code, so you can focus on the last 10% that makes your project special.
Requirements
- PHP 8.4 or higher
- Laravel 11.0 or 12.0
Installation
You can install the package via Composer as a dev dependency:
composer require --dev jcsoriano/laravel-crud-templates
The package will automatically register itself via Laravel's package discovery.
Publishing Stubs (Optional)
You can publish the stubs to customize them:
php artisan vendor:publish --tag="crud-templates-stubs"
The Command
To generate a CRUD feature, you may use this command:
php artisan crud:generate
{model : The name of the model}
{--fields= : The fields to generate. Format: field1:type1,field2?:type2}
{--table= : The database table to generate the fields from}
{--template=api : The CRUD template to generate}
{--skip= : List of files you want to skip}
{--options= : Other options to pass to the generator. Format: key1:value1,key2:value2}
{--force : Overwrite existing files}
Quick Start Example
Sample command to generate a fully functioning RESTful API:
php artisan crud:generate Content/Post --template=api --fields="title:string,content:text,published_at:datetime,category:belongsTo,comments:hasMany,status:enum:PublishStatus" --options="scope:user"
Generated Files
This command generates all the files needed to complete the CRUD feature, from routes, to validation, to authorization, to API responses, to migrations, to factories, to tests, and more.
app/Http/Controllers/Api/Content/PostController.phpapp/Models/Content/Post.phpapp/Policies/PostPolicy.phpapp/Http/Requests/Content/StorePostRequest.phpapp/Http/Requests/Content/UpdatePostRequest.phpapp/Http/Resources/Content/PostResource.phpdatabase/migrations/{timestamp}_create_posts_table.phpdatabase/migrations/{timestamp}_create_{pivot}_tables.php(if belongsToMany or morphToMany relationships are present)database/factories/Content/PostFactory.phptests/Feature/Api/Content/PostControllerTest.php- API routes automatically added to
routes/api.php(will runinstall:apiif the file doesn't exist yet) - Laravel Pint run on all generated files
Generated Routes
The command automatically registers the following routes in your routes/api.php file:
| HTTP Method | URI | Action | Description |
|---|---|---|---|
| GET | /api/posts |
index |
List all posts (paginated) |
| POST | /api/posts |
store |
Create a new post |
| GET | /api/posts/{id} |
show |
Show a specific post |
| PUT/PATCH | /api/posts/{id} |
update |
Update a post |
| DELETE | /api/posts/{id} |
destroy |
Delete a post |
Response Format
Single Resource:
{
"data": {
"id": 1,
"title": "My Post",
"content": "...",
"category": { "...": "..." },
"comments": [ { "...": "..." } ],
"status": "published",
"published_at": "2024-01-01T00:00:00.000000Z",
"created_at": "2024-01-01T00:00:00.000000Z",
"updated_at": "2024-01-01T00:00:00.000000Z"
}
}
Collection (with pagination):
{
"data": [
{ "The Post object as above" },
],
"links": { "first": "...", "last": "...", "prev": null, "next": "..." },
"meta": { "current_page": 1, "per_page": 15, "total": 50 }
}
Validation Rules
The request classes automatically include appropriate validation:
StorePostRequest:
public function rules(): array { return [ 'title' => ['required', 'string', 'max:255'], 'content' => ['required', 'string'], 'published_at' => ['required', 'date'], 'category_id' => ['bail', 'required', 'exists:categories,id'], 'status' => ['required', Rule::enum(PublishStatus::class)], ]; }
Model Enhancements
The generated Post model will include several automatic enhancements:
Relationship Methods:
public function category(): BelongsTo { return $this->belongsTo(Category::class); } public function comments(): HasMany { return $this->hasMany(Comment::class); }
Type Casting:
protected $casts = [ 'published_at' => 'immutable_datetime', // Automatic datetime casting 'status' => PublishStatus::class, // Enum casting ];
Fillable Fields:
protected $fillable = [ 'title', 'content', 'published_at', 'category_id', 'status', ];
Migration with Foreign Keys
The migration includes proper foreign key constraints:
Schema::create('posts', function (Blueprint $table) { $table->id(); $table->string('title'); $table->text('content'); $table->dateTime('published_at'); $table->foreignId('category_id')->constrained(); $table->string('status'); $table->timestamps(); });
API Resource
The generated PostResource automatically includes relationships:
public function toArray($request): array { return [ ... 'category' => CategoryResource::make($this->whenLoaded('category')), 'comments' => CommentResource::collection($this->whenLoaded('comments')), ]; }
Documentation
Getting Started
- Installation - Requirements and installation guide
- Quick Start - Generate your first CRUD in minutes
Available Templates
- API Template - RESTful API CRUD generation
- Creating Your Own Template - Create templates for your own use cases
Using Templates
- Field Types - Complete list of supported field types
- Relationships - Working with model relationships
- Generate from Schema - Generate from existing database tables
Customizing Templates
- Customizing Stubs - Modify stub templates
- Customizing Generators - Override file generators
- Customizing Field Types - Extend with custom field types
- Customizing Printers - Customize code output
Support
If you find this package useful and would like to support its development, consider supporting me through one of these platforms:
Your support helps me continue building and maintaining this package. Thank you! 🙏
Coming Soon
- Filament CRUD Generator - a Filament GUI for generating CRUD features
- Livewire CRUD Generator - a CRUD template built for the Livewire starter kit
- Vue CRUD Generator - a CRUD template built for the Vue starter kit
- React CRUD Generator - a CRUD template built for the React starter kit
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
License
The MIT License (MIT). Please see License File for more information.
