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 Ide Helper Laravel Package

barryvdh/laravel-ide-helper

Generates up-to-date PHPDoc helper files for Laravel to improve IDE autocomplete and type hints. Creates _ide_helper.php for facades and writes or exports model docblocks for Eloquent, fluent methods, factories, and container bindings.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Steps

  1. Installation:

    composer require --dev barryvdh/laravel-ide-helper
    
  2. Generate Facade PHPDocs (core functionality):

    php artisan ide-helper:generate
    

    This creates _ide_helper.php in your project root, enabling IDE autocompletion for Laravel Facades.

  3. Enable in IDE:

    • PhpStorm: Add _ide_helper.php to "Files to Parse" in Settings > Languages & Frameworks > PHP > PHPStorm.
    • VSCode: Ensure PHP Intelephense or similar extensions parse the helper files.

First Use Case

Debugging a Facade Method:

Route::get('/user', function () {
    return User::find(1)->toArray(); // Autocomplete works for `toArray()`
});

Without the helper, IDEs show no method suggestions for Facades. After running ide-helper:generate, PhpStorm/VSCode will autocomplete Facade methods like toArray(), first(), etc.


Implementation Patterns

1. Facade Autocompletion Workflow

  • Automate Generation: Add to composer.json to regenerate helpers on dependency updates:
    "scripts": {
        "post-update-cmd": [
            "@php artisan ide-helper:generate --force"
        ]
    }
    
  • Real-Time Facades: Use -M (SQLite in-memory) if your app lacks a default DB connection:
    php artisan ide-helper:generate -M
    
    Regenerate after using real-time facades (e.g., Route::cache()).

2. Model PHPDocs Workflow

  • Generate for All Models:
    php artisan ide-helper:models -RW
    
    • -R: Reset existing PHPDocs.
    • -W: Write directly to model files (avoids duplicates with --write-mixin).
  • Selective Generation:
    php artisan ide-helper:models App\Models\Post App\Models\User
    
  • Custom Directories:
    php artisan ide-helper:models --dir="app/Models" --dir="modules/User/Models"
    
    Configure defaults in config/ide-helper.php:
    'model_directories' => [
        app_path('Models'),
        database_path('Factories'),
    ],
    

3. Macros and Mixins

  • Type-Hint Macros (required for generation):
    Str::macro('slugify', function (string $str): string {
        return Str::of($str)->slug();
    });
    
    Regenerate helpers to include macro PHPDocs:
    php artisan ide-helper:generate
    

4. Fluent Methods (Migrations)

  • Enable in config/ide-helper.php:
    'include_fluent' => true,
    
    Regenerate:
    php artisan ide-helper:generate
    
    Now Schema::table()->string('name')->nullable() shows autocomplete for nullable().

5. PhpStorm Meta Files

  • Generate for Container Instances (e.g., factories):
    php artisan ide-helper:meta
    
    Publish the config to customize:
    php artisan vendor:publish --provider="Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider" --tag=config
    
    Add to .idea/misc.xml:
    <component name="ProjectRootManager" languageLevel="PHP_8_0">
      <output url="file://$PROJECT_DIR$/vendor/barryvdh/laravel-ide-helper/src/PhpStormMeta.php" />
    </component>
    

Gotchas and Tips

Pitfalls

  1. Database Connection Issues:

    • Command fails if no default DB connection exists. Use -M for SQLite in-memory:
      php artisan ide-helper:models -M
      
    • For CI/CD, mock the DB or use a test SQLite database.
  2. Duplicate PHPDocs:

    • Writing directly to models (-W) may cause duplicates. Use --write-mixin to avoid this:
      php artisan ide-helper:models --write-mixin
      
    • This adds @mixin IdeHelperPost to models and stores PHPDocs in _ide_helper_models.php.
  3. Real-Time Facades Not Generated:

    • Real-time facades (e.g., Route::cache()) require prior usage. Regenerate helpers after triggering them:
      php artisan route:cache
      php artisan ide-helper:generate
      
  4. IDE-Specific Quirks:

    • PhpStorm: Ensure _ide_helper.php is marked as "Generated" in .gitignore.
    • VSCode: Use the "PHP Intelephense" extension with includePaths configured:
      "intelephense.includePaths": [
          "./_ide_helper.php",
          "./_ide_helper_models.php"
      ]
      
  5. Model Hooks Overwriting:

    • Custom ModelHookInterface implementations may unintentionally override existing PHPDocs. Test hooks on a backup branch first.

Debugging Tips

  1. Verbose Output: Run with -v to debug generation:

    php artisan ide-helper:models -v
    

    Look for skipped models or errors.

  2. Check Generated Files: Inspect _ide_helper.php for missing Facades or incorrect return types. Example of a malformed entry:

    // WRONG: Missing return type
    @method static mixed find($id)
    

    Fix by editing the config or using a hook.

  3. Reset Cached Helpers: Delete _ide_helper.php and regenerate if changes aren’t reflected.

  4. IDE Cache:

    • PhpStorm: File > Invalidate Caches / Restart.
    • VSCode: Reload the window (Ctrl+Shift+P > "Reload Window").

Extension Points

  1. Custom Facade Resolutions: Override facade resolutions in config/ide-helper.php:

    'facades' => [
        'Auth' => \App\Services\CustomAuth::class,
    ],
    
  2. Additional Relation Types: Register custom relationships (e.g., for packages):

    'additional_relation_types' => [
        'hasCustom' => \Acme\HasCustom::class,
    ],
    
  3. Dynamic Property Generation: Use ModelHookInterface to add dynamic properties/methods:

    class DynamicHook implements ModelHookInterface {
        public function run(ModelsCommand $command, Model $model) {
            if ($model instanceof User) {
                $command->setProperty('api_token', 'string', false, false, 'User API token');
            }
        }
    }
    

    Register in config/ide-helper.php:

    'model_hooks' => [DynamicHook::class],
    
  4. Exclude Specific Methods: Disable magic where* methods globally:

    'write_model_magic_where' => false,
    

Performance

  • Large Projects: Exclude models with --ignore or configure ignored_models:
    'ignored_models' => [
        App\Models\Log::class,
        App\Models\Cache::class,
    ],
    
  • Parallel Generation: The package doesn’t support parallel processing, but you can split model generation across commands:
    php artisan ide-helper:models App\Models\User App\Models\Post
    php artisan ide-helper:models App\Models\Product App\Models\Order
    
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