🙌
Laravel 命名規則一覧まとめ
1. クラス・ファイル関連
対象 | 命名規則 | 良い例 | 悪い例 |
---|---|---|---|
コントローラ | 単数形 | ✅ ArticleController
|
❌ ArticlesController
|
モデル | 単数形 | ✅ User
|
❌ Users
|
マイグレーション | 規則に則った名称 | ✅ 2017_01_01_000000_create_articles_table
|
❌ 2017_01_01_000000_articles
|
FormRequest | 単数形(動作内容を明示) | ✅ UpdateUserRequest
|
❌ UpdateUserFormRequest , UserFormRequest
|
Seeder | 単数形 | ✅ UserSeeder
|
❌ UsersSeeder
|
契約(インターフェイス) | 形容詞または名詞 | ✅ AuthenticationInterface
|
❌ Authenticatable , IAuthentication
|
Trait | 形容詞(シンプルに) | ✅ Notifiable
|
❌ NotificationTrait
|
Trait (PSR準拠) | adjective(末尾にTrait を付ける) |
✅ NotifiableTrait
|
❌ Notification
|
Enum | 単数形 | ✅ UserType
|
❌ UserTypes , UserTypeEnum
|
2. ルート・ビュー・設定関連
対象 | 命名規則 | 良い例 | 悪い例 |
---|---|---|---|
ルート | 複数形 | ✅ articles/1
|
❌ article/1
|
名前付きルート | スネークケース+ドット表記 | ✅ users.show_active
|
❌ users.show-active , show-active-users
|
ビュー | ケバブケース(ハイフン区切り) | ✅ show-filtered.blade.php
|
❌ showFiltered.blade.php , show_filtered.blade.php
|
コンフィグファイル | スネークケース | ✅ google_calendar.php
|
❌ googleCalendar.php , google-calendar.php
|
設定/言語ファイルのインデックス | スネークケース | ✅ articles_enabled
|
❌ ArticlesEnabled , articles-enabled
|
3. データベース関連
対象 | 命名規則 | 良い例 | 悪い例 |
---|---|---|---|
テーブル | 複数形 | ✅ article_comments
|
❌ article_comment , articleComments
|
Pivotテーブル | 単数形(両モデル名をアルファベット順で連結) | ✅ article_user
|
❌ user_article , articles_users
|
テーブルカラム | スネークケース(モデル名は含めない) | ✅ meta_title
|
❌ MetaTitle , article_meta_title
|
外部キー | 単数形+_id (対象モデル名の単数形を利用) |
✅ article_id
|
❌ ArticleId , id_article , articles_id
|
主キー | 基本は id を使用 |
✅ id
|
❌ custom_id
|
4. リレーション
対象 | 命名規則 | 良い例 | 悪い例 |
---|---|---|---|
hasOne / belongsTo 関係 | 単数形 | ✅ articleComment
|
❌ articleComments , article_comment
|
その他のリレーション | 複数形 | ✅ articleComments
|
❌ articleComment , article_comments
|
5. メソッド・変数・テスト関連
対象 | 命名規則 | 良い例 | 悪い例 |
---|---|---|---|
メソッド | キャメルケース | ✅ getAll
|
❌ get_all
|
リソースコントローラのメソッド | フレームワーク定義済みの名称 | ✅ store
|
❌ saveArticle
|
テストクラスのメソッド | キャメルケース | ✅ testGuestCannotSeeArticle
|
❌ test_guest_cannot_see_article
|
変数 | キャメルケース | ✅ $articlesWithAuthor
|
❌ $articles_with_author
|
コレクション | 説明的かつ複数形 | ✅ $activeUsers = User::active()->get()
|
❌ $active , $data
|
オブジェクト | 説明的かつ単数形 | ✅ $activeUser = User::active()->first()
|
❌ $users , $obj
|
Discussion