0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Laravelで Undefined method 'items'.intelephense(P1013) を解決するための手順

Last updated at Posted at 2025-02-08

エラー

Undefined method 'items'.intelephense(P1013) のエラーは、PHP Intelephense が Laravel のリレーションを認識できていないことが原因 です。

この問題を解決するために laravel-ide-helper をインストールし、モデルの補完データを生成 していきます。

該当箇所

/Applications/MAMP/htdocs/Laravel/EXP-alert/app/Http/Controllers/ItemController.php
class ItemController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        // ⭐️items()で、Undefined method 'items'.intelephense(P1013)
        /** @var \App\Models\User $user */
        Auth::user()->items()->get();   

        return view('items.index');
    }

解決方法①

解決方法2つあります。
順番にご紹介していきます

1️⃣ laravel-ide-helper をインストール

composer require --dev barryvdh/laravel-ide-helper --with-all-dependencies

コマンドの意味

Laravel IDE Helper を開発環境 (--dev) にインストール します。
👉 Laravel のコード補完 (autocomplete) を強化し、Intelephense のエラー(P1013 など)を防ぐ。
👉--with-all-dependencies をつけることで 依存関係の競合を解決しながらインストール します。

オプション 説明
composer require パッケージをインストール
--dev 開発環境専用 (require-dev) にインストール
barryvdh/laravel-ide-helper Laravel の IDE 補助ツール
--with-all-dependencies 依存関係 (dependencies) も最新バージョンに更新

2️⃣ 依存関係の問題を解決(1️⃣でエラーが出た場合)

doctrine/dbal や carbonphp/carbon-doctrine-types の競合が原因で laravel-ide-helper のインストールに失敗することがあります。

🔹 競合しているパッケージを削除

composer remove carbonphp/carbon-doctrine-types

🔹 doctrine/dbal を手動インストール

composer require --dev doctrine/dbal

🔹 laravel-ide-helper を再インストール

composer require --dev barryvdh/laravel-ide-helper --with-all-dependencies

3️⃣ キャッシュをクリア

オートロードキャッシュや Laravel のキャッシュをクリアします。

php artisan clear-compiled
composer dump-autoload

キャッシュをリセットして ide-helper の設定を正しく適用する。

4️⃣ 補完データを生成

以下のコマンドを順番に実行します。

🔹 _ide_helper.php を生成

php artisan ide-helper:generate

このファイルには、Laravel の Facade やリレーションの補完情報が含まれます。
✅ Eloquent のモデル補完 (_ide_helper.php) を生成
✅ モデルの @property@method コメントを自動追加

🔹 _ide_helper_models.php を生成

php artisan ide-helper:models --write

モデルの補完データを作成し、Intelephense に認識させる。
✅ User や Collection に @method @property などの DocBlock コメントを追加
✅ Intelephense が collections() を正しく認識できるようになる

✅--write をつけると モデルの docblock に直接書き込む ので、エディタの補完が正確になります。

_ide_helper.meta.php を生成

php artisan ide-helper:meta

エディタの補完をより強化するメタ情報を作成する。

Eloquent の補完を追加

php artisan ide-helper:eloquent

✅ Eloquent のマジックメソッド (where(), find(), create(), update(), etc.) を IDE で補完!

5️⃣ Laravel のキャッシュをクリア

composer dump-autoload
php artisan cache:clear
php artisan config:clear

6️⃣ エディタのキャッシュをクリア

Ctrl + Shift + P(Mac: Cmd + Shift + P)
Intelephense: Index workspace を実行

エディタのキャッシュをクリアします。

7️⃣ _ide_helper.php を .gitignore に追加

このファイルは補完用のファイルであり、本番環境には不要なので .gitignore に追加します。

echo "_ide_helper.php" >> .gitignore
echo "_ide_helper_models.php" >> .gitignore

8️⃣ エラー解消を確認

エラーが出ていた items() のリレーションが認識されるか確認。

$user = Auth::user();
$items = $user->items()->get();
dd($items);

問題なく動けば成功!

✨ まとめ

手順 コマンド
1️⃣ laravel-ide-helper をインストール 「composer require --dev barryvdh/laravel-ide-helper --with-all-dependencies」
2️⃣ 依存関係の競合を解消(1️⃣エラー時) 「composer remove carbonphp/carbon-doctrine-types」「composer require --dev doctrine/dbal」
3️⃣ キャッシュをクリア 「php artisan clear-compiled」「composer dump-autoload」
4️⃣ 補完データを生成 「php artisan ide-helper:generate」「php artisan ide-helper:models --write」「php artisan ide-helper:meta」
5️⃣ Laravel のキャッシュをクリア 「composer dump-autoload」「php artisan cache:clear」「php artisan config:clear」
6️⃣ エディタのキャッシュをクリア 「Ctrl + Shift + P(Mac: Cmd + Shift + P)」→「Intelephense: Index workspace を実行」
7️⃣ _ide_helper.php を .gitignore に追加 「echo "_ide_helper.php" >> .gitignore」
8️⃣ エラー解消を確認 Auth::user()->items()->get();

解決方法②

PHP Intelephenseのダウングレード

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?