-
-
Notifications
You must be signed in to change notification settings - Fork 0
Advanced Usage
This guide covers advanced features and patterns for using Laravel TurboMaker effectively in complex projects.
Build complex relationship hierarchies by generating models in dependency order:
# E-commerce example with complex relationships
php artisan turbo:make Category --has-many=Product
php artisan turbo:make Brand --has-many=Product
php artisan turbo:make Product --belongs-to=Category --belongs-to=Brand --has-many=OrderItem --has-many=Review
php artisan turbo:make User --has-many=Order --has-many=Review
php artisan turbo:make Order --belongs-to=User --has-many=OrderItem --has-one=Payment
php artisan turbo:make OrderItem --belongs-to=Order --belongs-to=Product
php artisan turbo:make Review --belongs-to=Product --belongs-to=User
php artisan turbo:make Payment --belongs-to=Order
While TurboMaker doesn't auto-generate polymorphic relationships, you can set up the base structure and add polymorphic relationships manually:
# Generate base models
php artisan turbo:make Post --tests --factory
php artisan turbo:make Video --tests --factory
php artisan turbo:make Comment --tests --factory
# Manually add polymorphic relationships after generation
Comment.php (after generation):
public function commentable()
{
return $this->morphTo();
}
Post.php (after generation):
public function comments()
{
return $this->morphMany(Comment::class, 'commentable');
}
Structure your modules by domain:
# User Domain
php artisan turbo:make User --services --actions --policies --tests
php artisan turbo:make Role --belongs-to=User --services --tests
php artisan turbo:make Permission --tests
# Product Domain
php artisan turbo:make Product --services --actions --observers --tests --factory
php artisan turbo:make Category --has-many=Product --services --tests
php artisan turbo:make Inventory --belongs-to=Product --observers --tests
# Order Domain
php artisan turbo:make Order --belongs-to=User --services --actions --observers --tests
php artisan turbo:make OrderItem --belongs-to=Order --belongs-to=Product --tests
Organize by features rather than file types:
# Configure custom paths in config/turbomaker.php
'paths' => [
'models' => 'app/Features/{Feature}/Models',
'controllers' => 'app/Features/{Feature}/Http/Controllers',
'services' => 'app/Features/{Feature}/Services',
'actions' => 'app/Features/{Feature}/Actions',
],
Generate modules with comprehensive testing:
# Generate module with tests included
php artisan turbo:make Product --tests --factory --api --policies --services
# Or API with tests
php artisan turbo:api Product --tests --factory --policies --services
Generate complete test suites with all components:
php artisan turbo:make Order \
--belongs-to=User \
--has-many=OrderItem \
--tests \
--factory \
--seeder \
--policies \
--actions \
--services \
--observers
This generates tests for:
- Model relationships and validations
- Controller CRUD operations
- Policy authorization
- Service layer methods
- Action class execution
- Observer events
Build API-first applications:
# Generate API-only modules
php artisan turbo:api Product --tests --factory --policies
php artisan turbo:api Category --tests --factory
php artisan turbo:api Order --belongs-to=User --has-many=OrderItem --tests --policies
# Add views by generating full modules instead
php artisan turbo:make Product # Will include views by default
php artisan turbo:make Category
Create versioned API endpoints:
# Configure API versioning in config/turbomaker.php
'api' => [
'version_prefix' => 'v1',
'namespace' => 'App\\Http\\Controllers\\Api\\V1',
],
# Generate versioned controllers
php artisan turbo:api Product
Generate comprehensive API resources:
// ProductResource (generated and enhanced)
class ProductResource extends JsonResource
{
public function toArray($request)
{
return [
'id' => $this->id,
'name' => $this->name,
'price' => $this->price,
'category' => new CategoryResource($this->whenLoaded('category')),
'reviews' => ReviewResource::collection($this->whenLoaded('reviews')),
'average_rating' => $this->when($this->reviews_avg_rating, $this->reviews_avg_rating),
'created_at' => $this->created_at,
'updated_at' => $this->updated_at,
];
}
}
Generate models with relationship prevention:
// Generated model with optimizations
class Product extends Model
{
protected $with = ['category']; // Eager load by default
protected static function boot()
{
parent::boot();
// Prevent N+1 queries in development
if (app()->environment('local')) {
static::preventLazyLoading();
}
}
}
Generate optimized database structures:
# Generate with database optimizations
php artisan turbo:make Product --factory --seeder
# Add database indexes manually after generation
Migration (enhanced after generation):
public function up()
{
Schema::create('products', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->text('description');
$table->decimal('price', 10, 2);
$table->foreignId('category_id')->constrained()->cascadeOnDelete();
$table->boolean('is_active')->default(true);
$table->timestamps();
// Add indexes for performance
$table->index(['is_active', 'created_at']);
$table->index(['category_id', 'is_active']);
$table->fullText(['name', 'description']);
});
}
Use scripts to generate related modules:
generate-ecommerce.sh:
#!/bin/bash
# User management
php artisan turbo:make User --has-many=Order --has-many=Review --policies --tests --factory
php artisan turbo:make Role --tests --factory
php artisan turbo:make Permission --tests
# Product catalog
php artisan turbo:make Category --has-many=Product --tests --factory --seeder
php artisan turbo:make Brand --has-many=Product --tests --factory
php artisan turbo:make Product --belongs-to=Category --belongs-to=Brand --has-many=OrderItem --has-many=Review --tests --factory --seeder --policies --observers
# Order management
php artisan turbo:make Order --belongs-to=User --has-many=OrderItem --has-one=Payment --tests --factory --policies --services --actions --observers
php artisan turbo:make OrderItem --belongs-to=Order --belongs-to=Product --tests --factory
php artisan turbo:make Payment --belongs-to=Order --tests --factory --observers
# Reviews and ratings
php artisan turbo:make Review --belongs-to=Product --belongs-to=User --tests --factory --policies
echo "E-commerce modules generated successfully!"
Generate comprehensive test suites:
generate-tests.sh:
#!/bin/bash
# List of existing models
MODELS=("User" "Product" "Category" "Order" "OrderItem" "Payment" "Review")
for model in "${MODELS[@]}"; do
echo "Generating module with tests for $model..."
php artisan turbo:make "$model" --tests --factory --force
done
echo "All modules with tests generated!"
Integrate TurboMaker into your CI/CD pipeline:
generate-module.yml:
name: Generate Module
on:
workflow_dispatch:
inputs:
module_name:
description: 'Module name to generate'
required: true
include_tests:
description: 'Include tests'
type: boolean
default: true
jobs:
generate:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: 8.3
- name: Install dependencies
run: composer install
- name: Generate module
run: |
MODULE_NAME="${{ github.event.inputs.module_name }}"
INCLUDE_TESTS="${{ github.event.inputs.include_tests }}"
if [ "$INCLUDE_TESTS" = "true" ]; then
php artisan turbo:make "$MODULE_NAME" --tests --factory --policies
else
php artisan turbo:make "$MODULE_NAME" --factory --policies
fi
Ensure code quality with pre-commit hooks:
scripts/validate-generated-code.sh:
#!/bin/bash
# Run code style checks on generated files
./vendor/bin/pint --test
# Run static analysis
./vendor/bin/phpstan analyse
# Run tests
./vendor/bin/pest --parallel
echo "All generated code validation passed!"
Generate modules with event support:
# Generate modules with observers
php artisan turbo:make Order --observers --tests --actions --services
php artisan turbo:make Product --observers --tests
OrderObserver (enhanced after generation):
class OrderObserver
{
public function created(Order $order)
{
// Dispatch events
OrderCreated::dispatch($order);
// Send notifications
$order->user->notify(new OrderConfirmation($order));
// Update inventory
UpdateInventoryJob::dispatch($order);
}
public function updated(Order $order)
{
if ($order->wasChanged('status')) {
OrderStatusChanged::dispatch($order, $order->getOriginal('status'));
}
}
}
Generate modules with queue support:
php artisan turbo:make EmailNotification --services --actions --tests
Enable verbose output for debugging:
php artisan turbo:make Product --tests --factory -vvv
Handle file conflicts gracefully:
# Always overwrite (use with caution)
php artisan turbo:make Product --force
# Backup existing files before generation
cp -r app/Models/Product.php app/Models/Product.php.bak
php artisan turbo:make Product --force
- Define naming conventions
- Set up coding standards
- Configure TurboMaker defaults
- Always include tests and factories
- Use services for business logic
- Implement policies for authorization
- Generate related modules together
- Use scripts for batch generation
- Organize by business domains
- Include database indexes
- Prevent N+1 queries
- Use appropriate caching strategies
- Document generation patterns
- Use version control for configurations
- Establish code review processes
See the Examples page for complete implementations of:
- Complex E-commerce System - Multi-level relationships and advanced patterns
- API-First Application - Comprehensive API development
- DDD Implementation - Domain-driven design patterns
- Performance Optimized - Optimized for high-traffic applications
Laravel TurboMaker - Advanced Usage Guide
Laravel TurboMaker - Supercharge Your Laravel Development
🏠 Home | 📖 Getting Started | 💡 Examples | 🎮 Commands | 🔗 Relationships | ⚙️ Configuration
Navigation:
- 📚 Documentation - Complete guides and references
- 💡 Examples - Real-world examples and use cases
- 🛠️ GitHub Repository - Source code and issues
- 📦 Packagist - Package downloads
Community:
- 🐛 Report Issues - Bug reports and feature requests
- 💬 Discussions - Community discussions and Q&A
Made with ❤️ for the Laravel community | Version: 1.x | Laravel: 11.x-12.x | PHP: 8.3+
V2.1.0 - Fragment Architecture Revolution