DEV Community

loading...
Cover image for Laravel Best Practice [Coding Standards Part 01 ] πŸ§‘β€πŸ¦°πŸ‘©β€πŸ¦°

Laravel Best Practice [Coding Standards Part 01 ] πŸ§‘β€πŸ¦°πŸ‘©β€πŸ¦°

lathindu1 profile image Lathindu Pramuditha ・Updated on ・3 min read

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

Discussion (3)

pic
Editor guide
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 Pramuditha Author

Thank you very much Bobby lliev

Collapse
davebudah profile image
Dave Budah

I love the insight. Thank you.