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.
Installation:
composer require --dev barryvdh/laravel-ide-helper
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.
Enable in IDE:
_ide_helper.php to "Files to Parse" in Settings > Languages & Frameworks > PHP > PHPStorm.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.
composer.json to regenerate helpers on dependency updates:
"scripts": {
"post-update-cmd": [
"@php artisan ide-helper:generate --force"
]
}
-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()).php artisan ide-helper:models -RW
-R: Reset existing PHPDocs.-W: Write directly to model files (avoids duplicates with --write-mixin).php artisan ide-helper:models App\Models\Post App\Models\User
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'),
],
Str::macro('slugify', function (string $str): string {
return Str::of($str)->slug();
});
Regenerate helpers to include macro PHPDocs:
php artisan ide-helper:generate
config/ide-helper.php:
'include_fluent' => true,
Regenerate:
php artisan ide-helper:generate
Now Schema::table()->string('name')->nullable() shows autocomplete for nullable().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>
Database Connection Issues:
-M for SQLite in-memory:
php artisan ide-helper:models -M
Duplicate PHPDocs:
-W) may cause duplicates. Use --write-mixin to avoid this:
php artisan ide-helper:models --write-mixin
@mixin IdeHelperPost to models and stores PHPDocs in _ide_helper_models.php.Real-Time Facades Not Generated:
Route::cache()) require prior usage. Regenerate helpers after triggering them:
php artisan route:cache
php artisan ide-helper:generate
IDE-Specific Quirks:
_ide_helper.php is marked as "Generated" in .gitignore.includePaths configured:
"intelephense.includePaths": [
"./_ide_helper.php",
"./_ide_helper_models.php"
]
Model Hooks Overwriting:
ModelHookInterface implementations may unintentionally override existing PHPDocs. Test hooks on a backup branch first.Verbose Output:
Run with -v to debug generation:
php artisan ide-helper:models -v
Look for skipped models or errors.
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.
Reset Cached Helpers:
Delete _ide_helper.php and regenerate if changes aren’t reflected.
IDE Cache:
File > Invalidate Caches / Restart.Ctrl+Shift+P > "Reload Window").Custom Facade Resolutions:
Override facade resolutions in config/ide-helper.php:
'facades' => [
'Auth' => \App\Services\CustomAuth::class,
],
Additional Relation Types: Register custom relationships (e.g., for packages):
'additional_relation_types' => [
'hasCustom' => \Acme\HasCustom::class,
],
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],
Exclude Specific Methods:
Disable magic where* methods globally:
'write_model_magic_where' => false,
--ignore or configure ignored_models:
'ignored_models' => [
App\Models\Log::class,
App\Models\Cache::class,
],
php artisan ide-helper:models App\Models\User App\Models\Post
php artisan ide-helper:models App\Models\Product App\Models\Order
How can I help you explore Laravel packages today?