0
2

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.

modelとtableの命名規則

Posted at

1日エラーと戦い続け、原因がわからず、何度もコードを見直し…

お手上げ状態で質問したところ、なんとmodelとtableの命名規則を無視していた!

命名規則があること自体知らず、そんな大事なことはテキストに書いといてくれよ…
と思ったのでまとめておきます。

modelの命名規則

  • 単語の頭文字は大文字
  • 単語の連結も頭文字を大文字にする
  • 単数形

ex. ProfileHistory

tableの命名規則

  • 全て小文字
  • 単語の連結は"_"アンダーバーで行う
  • 複数形

ex. profile_histories

なぜ規則を破るとうまく動作しないのか

Laravelではmodel名から自動で参照すべきtable名を決めているので名前がおかしいとmodelとtableが結びつかない。

つまり、Laravelでは単語の頭文字が大文字になっている箇所で区切ったテーブル名を参照する。
ex. ProfileHistory(model名) → profile_histories(table名)

私がやってしまった間違えは
Profile_History(model名) → profile_histories(table名)

model名にアンダーバーを入れてしまったことで2単語を連結した名前だと認識されず、tableがありませんというエラーに繋がってしまった。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?