LaravelにIDE補完用ヘルパーファイル生成用のパッケージ「barryvdh/laravel-ide-helper」を導入する流れをまとめます。このパッケージで何ができるのかはこちらの記事を読んだ方が早いので引用します。

GitHub
本パッケージのGitHubはこちら。
導入方法
基本的にはパッケージのGitHubどおりに作業を進めればインストールできます。まずは以下コマンドでLaravelにインストールします。
omposer require --dev barryvdh/laravel-ide-helper
インストールするパッケージのバージョンを指定せずにコマンド(上記は指定なしです)を実行すると最新バージョンのパッケージがインストールされるので、Laravelのバージョンによっては以下のエラーが出ます。
Using version ^2.10 for barryvdh/laravel-ide-helper
./composer.json has been updated
Running composer update barryvdh/laravel-ide-helper
Loading composer repositories with package information
Updating dependencies
Your requirements could not be resolved to an installable set of packages.
Problem 1
- Root composer.json requires barryvdh/laravel-ide-helper ^2.10 -> satisfiable by barryvdh/laravel-ide-helper[v2.10.0].
- barryvdh/laravel-ide-helper v2.10.0 requires illuminate/console ^8 -> found illuminate/console[v8.0.0, ..., 8.x-dev] but these were not loaded, likely because it conflicts with another require.
Installation failed, reverting ./composer.json and ./composer.lock to their original content.
Laravel7以下のバージョンの場合は、本パッケージのリリース情報から該当のバージョンのパッケージをインストールしてください。
ちなみにLaravel7の場合は以下のコマンドでv2.8.2をインストールすれば導入できます。
composer require --dev barryvdh/laravel-ide-helper:"2.8.2"
composer.jsonのrequire-devに追加されたらインストール完了です。
"require-dev": {
"barryvdh/laravel-ide-helper": "^2.10", // バージョンは仮です。
},
基本的な使い方
これもGitHubどおりですが、基本的には以下の2つのコマンドを順番に実行するので良いです。
# PHPDoc generation for Laravel Facades
php artisan ide-helper:generate
# PHPDocs for models
php artisan ide-helper:models
2つ目のコマンド(php artisan ide-helper:models
)を実行すると、yesかnoの入力を求められます。
Do you want to overwrite the existing model files? Choose no to write to _ide_helper_models.php instead (yes/no) [no]:
- yes・・・モデルファイルに補完用コードが追加される
- no・・・
_ide_helper_models.php
という補完用の別ファイルを新規作成 or 更新
プロジェクトにも開発者にもよると思いますが、個人的にはモデルファイル(Modelsディレトリ配下のファイル)が補完用コードで汚くなるのでnoでいいと思います。(上の出力にもnoを推奨しているように見えます)
2つ目のコマンドのphp artisan ide-helper:models
はモデルファイルを追加した時、テーブルの仕様を変更した時などに都度実行する必要して補完用ファイルを更新する必要があるのですが、毎回yes、noを入力するのはめんどくさいです。
結論として、コマンド実行時にyes、noを決めた上で補間用ファイルを更新するためには以下のコマンドのどちらかを使えば良いです。(後者の-N
で良いかと)
# モデルファイルに直接書き込み
php artisan ide-helper:models -W
# _ide_helper_models.phpに書き込み
php artisan ide-helper:models -N
GitHubには
By default, you are asked to overwrite or write to a separate file (_ide_helper_models.php). You can write the comments directly to your Model file, using the –write (-W) option, or force to not write with –nowrite (-N).
と書かれており、以下のようなオプションが用意されています。
- yes・・・
--write
or-W
オプションをつける - no・・・
--nowrite
or-N
オプションをつける
--write
、--nowrite
より-W
、-N
の方が早く書けるので、以下を使えば良いかと。(繰り返しになりますが、基本的には-N
で良いと思います)
最後に
導入手順も簡単で、ORMを使って開発を行う時にはありがたく補完してくれるのでぜひ試してみてくだい。
コメント