Laravel IDE Helper is an extremely handy code hint and completion tool that makes writing code a lot easier.

Making stamp here

The installation

Install larave – ide – helper

Dev composer require barryvdh/ laravel-IDE-Helper dev composer require barryvdh/ laravel-ide-HelperCopy the code

Install Doctrine/DBAL “Please install it, it must be used when annotating fields for the model”

Dev Composer require "doctrine/dbal: ~2.3"Copy the code

Add it to the providers array of config/app.php

Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider::class
Copy the code

If your Laravel version is less than 5.5 “if MEMORY serves” please register the service provider, otherwise ignore it

If you only in the development environment to install “larave – ide – helper”, you can in the app/will/AppServiceProvider. PHP “method of” register “to write the following code:

public function register() { if ($this->app->environment() ! == 'production') { $this->app->register(\Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider::class); } / /... }Copy the code

Export the configuration file (you can skip this step if the default configuration is sufficient)

php artisan vendor:publish --provider="Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider" --tag=config
Copy the code

Okay, now you’re ready to use it happily

use

  • PHP artisan IDE-Helper :generate – Generates comments for Facades
  • PHP Artisan IDE – Helper: Models – Generate annotations for data models
  • PHP artisan IDE – Helper :meta – PhpStorm meta file is generated

Automatically generate comments for Laravel’s Facades

Run from the command line

php artisan ide-helper:generate
Copy the code

Note: If the bootstrap/compiled. PHP file needs to be deleted first, you can run PHP artisan clear-compiled when the file is generated.

Automatically generate annotations for the model

PHP artisan IDE-Helper: Models generates annotations for all models, and the following question appears:

Do you want to overwrite the existing model files? Choose no to write to _ide_helper_models.php instead? (Yes/No):  (yes/no) [no]:
Copy the code

Typing yes will write comments directly to the model file, otherwise the “_ide_helper_models.php” file will be generated. It is recommended to select yes so that you do not jump to the “_ide_helper_models.php” file when tracing the file, but it is best to make a backup of the model file and at least control the version with Git before generating comments, just in case.

Tip: Generate columns for your model only if you have tables in your database. Do not generate comments for migration before you have run Migrate.

Automatically annotate for chain operation

What does that mean? For example, it’s common to see code like this in a migration file:

$table->string('email')->unique();
Copy the code

Even after calling PHP artisan ide-helper:generate, you can’t prompt for a chain operation like ->unique(). Change the configuration file ‘include_FLUENT’ => false if exported to ‘include_fluent’ => true and run PHP artisan IDE-Helper :generate. Try the effect!

Generate the phpStorm. Meta. PHP

You can generate a PhpStorm meta file to support factory mode. For Laravel, this means that we can let PhpStorm understand what types of objects we have resolved from the IoC container. For example, the event will return a “Illuminate\Events\Dispatcher” object, and with the meta file you can call app(‘ Events ‘) and it will automatically complete the Dispatcher method.

app('events')->fire();
\App::make('events')->fire();

/** @var \Illuminate\Foundation\Application $app */
$app->make('events')->fire();

// When the key is not found, it uses the argument as class name
app('App\SomeClass');
Copy the code

Tip: you may need to restart Phpstorm for the.phpstorm.meta.php file to take effect.

Automatic operation generate

To update annotations automatically in dependent packages, you can do the following configuration in the composer. Json file:

"scripts":{
    "post-update-cmd": [
        "Illuminate\\Foundation\\ComposerScripts::postUpdate",
        "php artisan ide-helper:generate",
        "php artisan ide-helper:meta"
    ]
}
Copy the code

Tip: If you only deploy ide Helper in dev environment, don’t do this, to prevent errors in production environment and cause unnecessary trouble.

At the end

Larave-ide-helper and Doctrine/DBal: ~2.3 are all installed in Laravel 5.5 and above.

If you find any errors in this article, please! Axe! Is!!!