Query builder là giao diện fluent để xây dựng query mà không cần SQL thuần.
Ví dụ
DB::table("users")->where("age", ">", 18)->orderBy("name")->get();Ưu điểm: không phụ thuộc database (dễ chuyển đổi), tự động phòng SQL injection (tham số được escape), cú pháp chain dễ đọc, hỗ trợ IDE autocomplete. Các method: select(), where(), orWhere(), join(), groupBy(), having(), orderBy(), limit(), insert(), update(), delete(). So với SQL thuần DB::raw(...), query builder an toàn và dễ bảo trì hơn. Query builder là nền tảng của Eloquent.
Query builder is fluent interface for building queries without raw SQL.
Example
DB::table("users")->where("age", ">", 18)->orderBy("name")->get();Advantages: database-agnostic (easily switch databases), prevents SQL injection (parameters escaped automatically), readable chain syntax, IDE autocomplete support. Methods: select(), where(), orWhere(), join(), groupBy(), having(), orderBy(), limit(), insert(), update(), delete(). Compared to raw SQL DB::raw(...), query builder is safer and more maintainable. Query builder is foundation for Eloquent.