barryvdh/laravel-ide-helper
Generates accurate PHPDoc helper files for Laravel to improve IDE autocompletion and type hints. Create _ide_helper.php for facades, add or export model PHPDocs, fluent methods, factory builders, and PhpStorm container metadata—kept in sync with your project.
Installation:
composer require --dev barryvdh/laravel-ide-helper
First Use Case:
Generate PHPDocs for Laravel Facades (creates _ide_helper.php):
php artisan ide-helper:generate
Where to Look First:
_ide_helper.php in your project root.php artisan vendor:publish --provider="Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider" --tag=config) for customization.php artisan ide-helper:models to generate PHPDocs for Eloquent models (requires a working DB connection).Facades and Helpers:
Auth, Cache) via ide-helper:generate.composer.json for auto-generation on dependency updates:
"scripts": {
"post-update-cmd": [
"@php artisan ide-helper:generate",
"@php artisan ide-helper:meta"
]
}
Eloquent Models:
php artisan ide-helper:models --write # Writes to model files
php artisan ide-helper:models --nowrite # Generates _ide_helper_models.php
--write-mixin to avoid IDE duplicate warnings:
php artisan ide-helper:models --write-mixin
Custom Macros and Mixins:
Str::macro('concat', function(string $str1, string $str2): string)).php artisan ide-helper:generate
Fluent Methods (Migrations):
$table->string()->nullable()):
// config/ide-helper.php
'include_fluent' => true,
php artisan ide-helper:generate
PhpStorm Meta Files:
php artisan ide-helper:meta
php artisan vendor:publish --provider="Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider" --tag=meta
ide-helper:generate in CI to ensure PHPDocs are up-to-date before merging._ide_helper.php and _ide_helper_models.php to version control (exclude from .gitignore).Settings > Languages & Frameworks > PHP.ide-helper support enabled.Database Connection Required:
ide-helper:models fails if the default DB connection is misconfigured. Use -M for SQLite:
php artisan ide-helper:models -M
Real-Time Facades:
Route::get()) require prior usage to generate cache files in storage/framework/cache. Regenerate helpers after triggering them.Duplicate PHPDocs:
--write) may cause conflicts with existing PHPDocs. Use --reset to overwrite:
php artisan ide-helper:models --write --reset
Custom Relationships:
hasManyThrough) require config definitions:
'additional_relation_types' => [
'hasManyThroughCustom' => \Custom\HasManyThrough::class,
],
IDE Caching:
File > Invalidate Caches) after regenerating helpers.Missing Facade Methods:
config/ide-helper.php for facades overrides or missing class mappings.Auth → Illuminate\Auth\AuthManager).Model PHPDocs Not Updating:
--write or --nowrite is used correctly. Reset with --reset if needed.'ignored_models' => [App\IgnoredModel::class],
Fluent Methods Not Recognized:
include_fluent is true in config and regenerate helpers.Model Hooks:
ModelHookInterface:
class CustomHook implements ModelHookInterface {
public function run(ModelsCommand $command, Model $model): void {
$command->setProperty('custom_field', 'string');
}
}
config/ide-helper.php:
'model_hooks' => [CustomHook::class],
Custom Generics:
Collection<User>) for older IDEs:
'use_generics_annotations' => false,
Excluding Magic Methods:
where* methods or *_count properties:
'write_model_magic_where' => false,
'write_model_relation_count_properties' => false,
Helper File Naming:
'filename' => 'custom_ide_helper.php',
'models_filename' => 'custom_models_helper.php',
Partial Generations:
php artisan ide-helper:models App\Models\Post # Single model
php artisan ide-helper:generate --facades-only # Facades only
Git Ignore:
.gitignore if they’re auto-generated:
/_ide_helper.php
/_ide_helper_models.php
IDE-Specific Annotations:
@method for custom query scopes:
/**
* @method static \Illuminate\Database\Eloquent\Builder<static>|Post published()
*/
Performance:
php artisan ide-helper:models --ignore="App\Models\LargeModel"
How can I help you explore Laravel packages today?