2
1

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 1 year has passed since last update.

[Laravel]"Cannot declare class (クラス名), because the name is already in use"のエラー対処について

Last updated at Posted at 2023-01-12

開発環境:Laravel8

事象

  • Controllerを新規作成をして、アクションを定義する。その後ルーティングを設定して、ブラウザからアクションを実行した際、下記のエラーが発生しました。
Cannot declare class (クラス名), because the name is already in use
  • エラー文の意味は、「クラス名が既に使用されているため、クラス名を宣言できません。」
  • つまり、「そのクラス名はすでに使われているので、クラス名を使用することが出来ませんよ」ということです。

今回、Contollerのクラス名が重複しているということだったので、エラー文でのクラス名が重複していないか他のControllerのクラス名を確認しました。
しかし、該当するクラス名は重複していませんでした。

原因

  • エラーの発生原因は、Controller内での名前空間(namespace)でのコーディングが間違っていました。
// 間違っていたコード
namespace App\Http\Controllers;

解決方法

  • 正しい名前空間(namespace)に修正したところ、エラーが解決しました。
// 正しいコード(一階層間違えていたので、修正)
namespace App\Http\Controllers\Doctor;

上記のようなエラーが発生した場合、「名前空間(namespace)」でのミスがないか確認をして下さい。

2
1
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
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?