Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Laravel Stubs Laravel Package

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.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. 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.

  2. 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.


Implementation Patterns

Workflows

  1. 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
    
  2. 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).

  3. Integration with IDE/Editor: Leverage the stubs to ensure IDE autocompletion and hints match the generated code (e.g., return types in controllers).

  4. 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").


Integration Tips

  • 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.


Gotchas and Tips

Pitfalls

  1. Missing down() Methods:

    • Issue: Migrations lack rollback logic, which may break deployments relying on php artisan migrate:rollback.
    • Fix: Manually add down() to critical migrations or use a migration helper package like spatie/laravel-migration-skeleton.
  2. No Guarded Attributes:

    • Issue: Models expose all attributes by default, risking mass assignment vulnerabilities.
    • Fix: Explicitly define $guarded or use $fillable in models:
      protected $fillable = ['name', 'email'];
      
  3. Removed Docblocks:

    • Issue: IDEs may flag missing PHPDoc annotations for new developers.
    • Fix: Add a pre-commit hook to auto-generate docblocks for critical classes (e.g., using phpDocumentor).
  4. Base Controller Absence:

    • Issue: Shared middleware or logic (e.g., auth checks) must be duplicated.
    • Fix: Use traits or middleware for reusable logic:
      use App\Traits\HandlesAuthorization;
      
      class UserController {
          use HandlesAuthorization;
      }
      

Debugging

  • 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
    

Extension Points

  1. 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);
    
  2. Event-Based Customization: Listen to stub-generated events to post-process stubs:

    // In EventServiceProvider
    protected $listen = [
        'stub-generated' => [
            'App\Listeners\InjectCustomLogic',
        ],
    ];
    
  3. Partial Adoption: Mix default and custom stubs by selectively publishing tags:

    php artisan stub:publish --tag=model,controller
    

Pro Tips

  • 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.

Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport