DEV Community

Cover image for Laravel Best Practice [Coding Standards Part 01 ] ๐Ÿง‘โ€๐Ÿฆฐ๐Ÿ‘ฉโ€๐Ÿฆฐ
Lathindu Pramduitha
Lathindu Pramduitha

Posted on • Updated on

Laravel Best Practice [Coding Standards Part 01 ] ๐Ÿง‘โ€๐Ÿฆฐ๐Ÿ‘ฉโ€๐Ÿฆฐ

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

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

Other Articles

  • MetaMask Integration With Laravel Part1 Click Here

  • MetaMask Integration With Laravel Part2 Click Here

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


Thank You Very much.
--Lathindu Pramuditha--

GitHub Profile

GitHub logo lathindu1 / lathindu1

It's About Lathindu Pramuditha's Account

เถ†เถบเท”เถถเทเท€เถฑเทŠ (Welcome)๐Ÿ™๐Ÿป, I'm Lathindu Pramuditha Amarasekara!

Software Engineer at Speralabs

Linkedin: lathindu-pramuditha GitHub followers Waka Readme

A little more about me...

namespace App\Models
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Life;

class ProfileOfLathindu extends Life
{
    use HasFactory;
    const LANGUAGES = [
        'PHP' => 1,
        'JAVASCRIPT' => 2,
        'PYTHON' => 3,
        'SOLIDITY' => 4,
        'DART' => 5
    ];

    const FRAMEWORKS = [
        'LARAVEL' => 1,
        'FLUTTER' => 2,
        'DJANGO' => 3,
        'ANGULAR' => 4,
        'IONIC' => 5
    ];

    const EXPERIENCE = 'xxxxxxxxxx of hours from 2017';

    const MORE_EXPERIENCE = [
        'PAYPAL_API' => 1,
        'STRIPE_API' => 2,
        'PAYHERE_SDK' => 3,
        'UPHOLD_API' => 4,
        'VIMEO_SDK' => 5,
        'NMI_API' => 6,
        'SENDGRID_API' => 7,
        'AWEBER_API' => 8,
        'GETRESPOND_API' => 9,
        'REMIX' => 10,
        'BTCPAY_SERVER'
โ€ฆ
Enter fullscreen mode Exit fullscreen mode

Alt Text

Top comments (3)

Collapse
 
bobbyiliev profile image
Bobby Iliev

Great post! You should repost it on the DevDojo site and you will have the chance to win a weekly prize!

Collapse
 
lathindu1 profile image
Lathindu Pramduitha

Thank you very much Bobby lliev

Collapse
 
davebudah profile image
Dave Budah

I love the insight. Thank you.