Last active
July 5, 2024 00:18
-
-
Save Leotrimlota/8c3df97c655d782eda715315b8f8810f to your computer and use it in GitHub Desktop.
Naming convention PHP Laravel
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| What | How | Good | Bad | |
|---|---|---|---|---|
| Controller | singular | ArticleController | ArticlesController | |
| Route | plural | articles/1 | article/1 | |
| Named route | snake_case with dot notation | users.show_active | users.show-active, show-active-users | |
| Model | singular | User | Users | |
| hasOne or belongsTo relationship | singular | articleComment | articleComments, article_comment | |
| Table | plural | article_comments | article_comment, articleComments | |
| Pivot table | singular model names in alphabetical order | article_user | user_article, articles_users | |
| Table column | snake_case without model name | meta_title | MetaTitle, article_meta_title | |
| Model property | snake_case | $model->created_at | $model->createdAt | |
| Foreign key | singular model name with _id suffix | article_id | ArticleId, id_article, articles_id | |
| Primary key | - | id | custom_id | |
| Method in resource controller | table | store | saveArticle | |
| Method in test class | camelCase | testGuestCannotSeeArticle | test_guest_cannot_see_article | |
| Collection | descriptive & plural | $activeUsers = User::active()->get() | $active, $data | |
| Object | descriptive & singular | $activeUser = User::active()->first() | $users, $obj | |
| Config and language files index | snake_case | articles_enabled | ArticlesEnabled, articles-enabled | |
| Contract (interface) | adjective or noun | Authenticatable | AuthenticationInterface, IAuthentication | |
| Trait | adjective | Notifiable | NotificationTrait |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment