Laravel Best Practice Standard

Laravel Best Practice Standard

Laravel is web application framework dengan syntax which is expressive and elegant. web application framework provides a structure and starting point for creating your app, allowing you to focus on building something while thinking about the details.


#1. Prinsip Single Responsibility

Classes and methods should have only one responsibility.

#2. Fat Models, Skinny Controller

Insert all DB-related logic into the model eloquent atau ke dalam kelas repositori jika anda menggunakan Query Builder.

#3. Validation

Move validation from Controller ke Class Request.

#4. Don’t Repeat Yourself (DRY)

When programming, Avoid duplicating code as much as possible. The Single Responsibility principle helps you avoid duplication by ensuring each piece of code has a single responsibility. Moreover Take advantage of features such as reuse of blade templates, use of scopes on Eloquent, and more to maximize efficiency and consistency in your app development.

#5. Use Eloquent, instead of Query Builder / Raw Query

Eloquent Allows you to write readable code and maintainable. Dan, Eloquent have built-in tools yang bagus seperti soft deletes, events, scopes, dll.

#6. Mass Assignment

Sending an array to model creation, is essentially setting multiple fields on the model at once, rather than one at a time.

#7. Follow the Laravel Naming Convention

follow Prinsip single responsibility and follow the naming conventions accepted by the Laravel community.

#8. Use Middleware instead of Repeating Code

Middleware in Laravel allows you to filter or change Request saat ini. Berikut beberapa kasus penggunaan:

  • Check the required permissions
  • Check the user's language and change the locale

And as you'd expect, Laravel comes with a bunch of ready-to-use middleware for authentication, speed limiting, and more.

Once your middleware is doing what it's supposed to do, you can block it Request tersebut atau membiarkannya lewat.

Middleware can be attached to any number of routes, which helps you prevent code duplication.

Berita Rekomendasi

Strategi Tepat Memilih Metode Deployment

19/02/2025

Strategi Tepat Memilih Metode Deployment

Pemilihan strategi tepat dalam memilih Metode deployment dalam Continuous Integration/Continuous Deployment (CI/CD) berperan penting dalam memastikan bahwa perubahan kode dapat diterapkan dengan cepat, aman, dan tanpa gangguan pada pengguna akhir.…

View
Vibe Coding: Cara Baru Membangun Software di Era AI

13/03/2026

Vibe Coding: Cara Baru Membangun Software di Era AI

Dalam beberapa tahun terakhir, dunia pengembangan software mengalami perubahan besar. Jika dulu programmer harus menulis kode baris demi baris, kini muncul pendekatan baru yang disebut vibe coding, yaitu cara membuat…

View
Unlocking Real-Time Insights with Metabase

24/07/2025

Unlocking Real-Time Insights with Metabase

Apa Itu Metabase? Metabase adalah platform open-source Business Intelligence (BI) yang memungkinkan siapa saja di dalam organisasi—teknis maupun non-teknis—untuk mengakses, mengeksplorasi, dan mengambil keputusan berbasis data secara real-time. Dengan tampilan…

View