spatie/laravel-stubs
Opinionated Laravel stub templates from Spatie. Publish customized stubs for migrations, controllers, and models: no down() in migrations, controllers don’t extend a base, no guarded attributes, more return types, fewer docblocks.
Installation:
composer require spatie/laravel-stubs
No additional configuration is required—just publish the stubs by running:
php artisan stub:publish
This replaces Laravel’s default stubs in resources/stubs.
First Use Case:
Generate a new migration without a down() method:
php artisan make:migration create_users_table
The generated file will omit the down() method by default, aligning with the package’s opinionated approach.
Generate a controller without extending a base class:
php artisan make:controller UserController
The stub will skip inheritance and use return type hints.
Consistent Code Generation:
Use the package’s stubs for all make: commands (e.g., make:model, make:controller, make:middleware) to enforce a standardized, minimalist style across the team.
Example:
php artisan make:model Post -m -r # Creates a model with migration and resource stubs
Customizing Stubs: Override specific stubs by publishing them first, then modifying:
php artisan stub:publish --tag=model
Edit resources/stubs/model.stub to add custom logic (e.g., auto-injecting traits or interfaces).
Integration with IDE/Editor: Leverage the stubs to ensure IDE autocompletion and hints match the generated code (e.g., return types in controllers).
Team Onboarding:
Document the package in your team’s style guide to explain why stubs are opinionated (e.g., "No down() methods to reduce merge conflicts").
Testing:
Extend Laravel’s CreateUserTest stub to match your auth flow:
php artisan stub:publish --tag=test
Modify resources/stubs/test.stub to include your custom assertions.
CI/CD: Add a linting step to enforce stub-generated code adheres to your PSR standards (e.g., no docblocks).
Legacy Code:
Use php artisan stub:publish to audit existing stubs and migrate to the new format incrementally.
Missing down() Methods:
php artisan migrate:rollback.down() to critical migrations or use a migration helper package like spatie/laravel-migration-skeleton.No Guarded Attributes:
$guarded or use $fillable in models:
protected $fillable = ['name', 'email'];
Removed Docblocks:
phpDocumentor).Base Controller Absence:
use App\Traits\HandlesAuthorization;
class UserController {
use HandlesAuthorization;
}
Stub Overrides Not Applying: Ensure stubs are published before generating new files. Clear cached stubs with:
php artisan cache:clear
composer dump-autoload
Custom Stubs Breaking: Test stub changes in a fresh Laravel install to isolate issues:
php artisan make:model TestModel
Dynamic Stubs:
Use Laravel’s stub helper to generate dynamic content:
$stub = file_get_contents(resource_path('stubs/migration.stub'));
$migration = str_replace(['{{table}}'], [$tableName], $stub);
Event-Based Customization:
Listen to stub-generated events to post-process stubs:
// In EventServiceProvider
protected $listen = [
'stub-generated' => [
'App\Listeners\InjectCustomLogic',
],
];
Partial Adoption: Mix default and custom stubs by selectively publishing tags:
php artisan stub:publish --tag=model,controller
Enforce Consistency:
Add a Git pre-commit hook to validate stub-generated files against a regex pattern (e.g., no extends App\Http\Controllers\Controller).
Performance: Cache compiled stubs in production by setting:
'stubs_cache' => true,
in config/stubs.php.
Education: Link the Spatie postcard wall in your team’s Slack channel to encourage adoption.
How can I help you explore Laravel packages today?