explaingit

alexeymezenin/laravel-best-practices

12,276Audience · developerComplexity · 1/5Setup · easy

TLDR

A curated guide to writing clean, maintainable Laravel PHP code, with side-by-side bad vs. good examples for patterns like service classes, Eloquent, and naming conventions.

Mindmap

mindmap
  root((laravel-best-practices))
    What it does
      Side-by-side examples
      Bad vs good patterns
      20+ languages
    Key Principles
      Single responsibility
      Fat models thin controllers
      Service classes
      Eloquent over raw SQL
    Performance
      N+1 problem
      Chunking large data
    Code Style
      Naming conventions
      Config files
      No HTML in PHP
Click or tap to explore — scroll the page freely

Code map

Detail Auto

An interactive map of this repo's files and how they connect — its source is parsed live in your browser. Click Visualize to build it.

filefunction / class

Things people build with this

USE CASE 1

Follow proven patterns to structure a Laravel app so other developers can read and maintain it easily

USE CASE 2

Avoid the N+1 query performance trap when loading related data in Laravel pages

USE CASE 3

Move validation, business logic, and database queries into the right classes as experienced Laravel developers expect

Tech stack

PHPLaravel

Getting it running

Difficulty · easy Time to first run · 5min

In plain English

This repository is a guide to writing clean, well-organized code in Laravel, a popular PHP framework used to build web applications. Rather than teaching Laravel from scratch, it collects a set of opinionated rules and patterns that experienced Laravel developers follow to keep projects readable and easy to maintain over time. Each section in the guide focuses on one principle, usually illustrated with two code examples side by side: one labeled "Bad" showing a common mistake, and one labeled "Good" showing the recommended approach. The principles covered include keeping each class or method focused on a single task, putting database query logic inside the data model instead of the controller, moving validation rules into their own classes, and placing business logic in dedicated service classes rather than scattering it throughout the application. Other sections address more specific habits: avoiding repeated code, using Laravel's built-in data querying tool called Eloquent instead of raw SQL, and preventing a common performance trap known as the N+1 problem (where a page accidentally fires dozens of database queries by loading related data one piece at a time). There are also sections on chunking large data sets to avoid memory limits, using descriptive method and variable names, keeping HTML and CSS out of server-side template files, storing configuration values in dedicated files rather than hardcoding strings in code, and following Laravel's own naming conventions so that any developer familiar with the framework can read any Laravel project without extra context. The guide is available in over 20 languages, including Chinese, Japanese, Russian, Portuguese, Arabic, and several others, reflecting how widely read it is across the global developer community.

Copy-paste prompts

Prompt 1
How do I refactor this Laravel controller to move business logic into a service class, following the laravel-best-practices guide?
Prompt 2
Show me how to fix an N+1 query problem in my Laravel app using Eloquent eager loading as recommended in alexeymezenin/laravel-best-practices
Prompt 3
How should I use Laravel Form Requests to move validation out of my controller per the laravel-best-practices guide?
Prompt 4
What naming conventions should my Laravel models, controllers, and methods follow according to alexeymezenin/laravel-best-practices?
Open on GitHub → Explain another repo

← alexeymezenin on gitmyhub — every repo by this author, as a profile.

Verify against the repo before relying on details.