6
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Laravel 独自のTraitが読み込まれない場合に確認してみたこと

Last updated at Posted at 2021-02-03

Laravelで Trait 'App\Traits\CustomTrait' not found というエラーが出て、
何回コードを見ても絶対あっているはずなのに、not foundと言われます。

その際に確認することをメモとして残しておきます。

※コメントでご指摘をいただいていますので、そちらも併せて見てください!

/vendor/composer/autoload_classmap.phpをみる

TraitのディレクトリがAutoloadに読み込まれているか確認します。
(autoloadは名前空間とパスの対応表みたいな感じになっています。)

僕の場合はTraitを置いていたパスが app\Traitでした。

そのファイルの中に、下記の文言があるか確認します。


App\\Trait\\CustomTrait

ここでファイルの中に存在する場合は、ただのコードのミスのような気がしますが、
僕の場合はここに記述がありませんでした。

composer.json に追記する

composer.jsonのautoloadの部分に、Traitを置いているディレクトリを追記します。


"autoload": {
    "classmap": [
        "database/seeds",
        "database/factories",
        "app/Trait"  // これを追記する
    ],
    "psr-4": {
        "App\\": "app/"
    }
},

composer dump-autoload をする

composer dump-autoloadで設定を更新します。

composer dump-autoload

これで、Traitがみつかるようになりました!!

おわりに

なかなかいろいろ調べて、解決するのが大変でしたので、
他の方の支えになれば幸いです。

6
3
1

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
6
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?