Laravel Best Practice [Coding Standards Part 01 ] 🧑‍🦰👩‍🦰
Tags: laravel, beginners, php, codequality
Published: 2021-03-21 18:29:49

**Hi guys,** From this article I'm starting to describe you to about Laravel [PHP] coding Standards with **PSR**.

To make this tutorials much easier, I will divide full tutorial as number or articles. So this is first articles of them.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lazc5hhniemwwfjbjbrk.gif)
##Article 1 : Naming Conventions. ✊

Here we will talk about naming conventions about PHP. Following conventions have accepted by Laravel community.

####01.01 Controller 👈

- Name should be in **singular** form.
- Should use **PascalCase**.

|       Should Do         |            Should't Do          |
|-------------------------|---------------------------------|
| `CustomerController.php`  |   `CustomersController.php`   |

_____________________________________________________________


####01.02 Route 👈


#####01.02.01 Route Url 👈

- Url should be in **plural** form.
- Can use **kebab-case** if there are two words in single part For best Practice.


|       Should Do         |            Should't Do          |
|-------------------------|---------------------------------|
| `/customers/25`         |   `customer/25`                 |
| `/customers/password-reset` | `/customers/password_reset` |
| `"`                     | `/customers/passwordReset`      |



#####01.02.02 Route Name 👈

- Should use **snake_case** with dot notation.
- Better to use same name like in URL.

|       Should Do         |            Should't Do          |
|-------------------------|---------------------------------|
| `->('customers.view')`  |   `->('customers-view')`   |
| `"`  |   `->('customers_view')`   |
| `->('customers.password_reset')`|`->('customers.password.reset')`|
| `"`|`->('customers.password-reset')`|
| `"`|`->('customer-password-reset')`|


_____________________________________________________________

####01.03 DataBase Related 👈

#####01.03.01 Migration 👈

- Should use name as what you want to do with **snake_case**.

|       Should Do         |            Should't Do          |
|-------------------------|---------------------------------|
| `2021_03_19_033513_create_customers_table.php` | `2021_03_19_033513_customers.php` |
| `2021_03_19_033513_add_image_id_to_customers_table.php` | `2021_03_19_033513_add_image_id_customers.php` |
| `2021_03_19_033513_drop_image_id_from_customers_table.php` | `2021_03_19_033513_remove_image_id_customers.php` |



#####01.03.02 Table 👈

- Table name must be in **plural** form.
- Should use **snake_case**.

|       Should Do         |            Should't Do          |
|-------------------------|---------------------------------|
| `customers` | `customer` |
| `cart_items` | `cartItems` , `CartItems` , `Cart_item` |



#####01.03.03 Pivot Table 👈

- Table name must be in **singular** form.
- Should use **snake_case**
- Names should be in **alphabetical** Order.

|       Should Do         |            Should't Do          |
|-------------------------|---------------------------------|
| `course_student` | `student_courses` , `students_courses` ,`course_students` |


#####01.03.04 Table Columns 👈

- Should use **snake_case**.
- Should not use table name with column names.
- Readable name can use for better practice.

|       Should Do         |            Should't Do          |
|-------------------------|---------------------------------|
| `first_name` | `user_first_name` , `FirstName` |




#####01.03.05 Foreign key 👈

- Should use **snake_case**.
- Should use **singular** table name with **id** prefix.

|       Should Do         |            Should't Do          |
|-------------------------|---------------------------------|
| `course_id` | `courseId` , `id` ,`courses_id` ,`id_course` |



#####01.03.06 Primary key 👈

- only use name as **id**.

|       Should Do         |            Should't Do          |
|-------------------------|---------------------------------|
| `id` | `custom_name_id` |



#####01.03.07  Model 👈

- Model name must be in **singular** form.
- Should Use **PascalCase**
- Model name must be a singular form or table name.

|       Should Do         |            Should't Do          |
|-------------------------|---------------------------------|
| `Customer` | `Customers`,`customer` |



#####01.03.08  Model Single relations [Has One, Belongs To] 👈

- Method name must be in **singular** form.
- Should Use **camalCase**

|       Should Do         |            Should't Do          |
|-------------------------|---------------------------------|
| `studentCourse` | `StudentCourse `,`student_course`,`studentCourses` |



#####01.03.09  Model all other relations and methods [Has Many,other] 👈

- Method name must be in **plural** form.
- Should use **camalCase**

|       Should Do         |            Should't Do          |
|-------------------------|---------------------------------|
| `cartItems` | `CartItem `,`cart_item`,`cartItem` |

_____________________________________________________________



####01.04  Functions 👈

- Should Use **snake_case**

|       Should Do         |            Should't Do          |
|-------------------------|---------------------------------|
| `show_route` | `showRoute `,`ShowRoute` |


_____________________________________________________________




####01.05  Methods in resources controller 👈

- Should use **camelCase**
- Must use singles words related to action

|       Should Do         |            Should't Do          |
|-------------------------|---------------------------------|
| `store` | `saveCustomer` |
| `show` | `viewCustomer` |
| `destroy` | `deleteCustomer` |
| `index` | `allCustomersPage` |


_____________________________________________________________



####01.06  Variables 👈

- Should use **camelCase**
- Must use readable words which are describe about value.

|       Should Do         |            Should't Do          |
|-------------------------|---------------------------------|
| `$customerMessages` | `$CustomerMessages` ,`$customer_messages` , `$c_messages` , `$c_m` |

_____________________________________________________________



####01.07  Collection 👈

- Must described about the value.
- Must be plural


|       Should Do         |            Should't Do          |
|-------------------------|---------------------------------|
| `$verifiedCustomers = $customer->verified()->get()` | `$verified` ,`$data` , `$resp` , `$v_c` |

_____________________________________________________________



####01.07  Object 👈

- Must described about the value.
- Must be singular


|       Should Do         |            Should't Do          |
|-------------------------|---------------------------------|
| `$verifiedCustomer = $customer->verified()->first()` | `$verified` ,`$data` , `$resp` , `$v_c` |

_____________________________________________________________



####01.08  Configs 👈

- Should use **snake_case**
- Must described about the value.

|       Should Do         |            Should't Do          |
|-------------------------|---------------------------------|
| `comments_enabled` | `CommentsEnabled` ,`comments` , `c_enabled` , `$ce` |

_____________________________________________________________



####01.09  Traits 👈

- Should be adjective.

|       Should Do         |            Should't Do          |
|-------------------------|---------------------------------|
| `Utility` | `UtilityTrait` ,`Utilities` |

_____________________________________________________________



####01.10  Interface 👈

- Should be adjective or a noun.

|       Should Do         |            Should't Do          |
|-------------------------|---------------------------------|
| `Authenticable` | `AuthenticationInterface` ,`Authenticate` |

So above I have talked about naming convetion in Laravel projects. not only Laravel you guys can use this rules with any other PHP framework. 



With next article I will talk about another main topic of coding standards. 

I hope you find my post useful! Any feedback is greatly appreciated!

below I mentioned my other articles. you may also read them.

##You may Find my Fiver Gig Here.

[https://www.fiverr.com/s2/0c68721323](https://www.fiverr.com/s2/0c68721323)

#Other Articles

- MetaMask Integration With Laravel Part1 [Click Here](https://dev.to/lathindu1/metamask-integration-with-laravel-4mng)

- MetaMask Integration With Laravel Part2 [Click Here](https://dev.to/lathindu1/validate-metamask-transactions-with-etherscan-metamask-integration-with-laravel-part-2-25h6)


Here I'm Adding Public GitHub Repository which will store all of my tutorials. you may clone it and see every tutorials what I will publish 🤗.

**GitHub Repository** {% github lathindu1/tutorials %}

**Thank You Very much**.
--Lathindu Pramuditha--


**GitHub Profile** {% github lathindu1/lathindu1 %}

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tto8nw2ig5iadout9zl8.png)