What is CodeIgniter 4's Query Builder?
Answer
The Query Builder (formerly Active Record in CI3) is CI4's fluent interface for constructing database queries. Access from a model: $this->db or $db = \Config\Database::connect(). Methods: $db->table("users")->select("name, email")->where("active", 1)->orWhere("role", "admin")->whereIn("department", [1, 2])->like("name", "Al")->orderBy("name", "ASC")->limit(10, 20)->get(). Join: ->join("profiles", "profiles.user_id = users.id", "left"). Group by: ->groupBy("department")->having("count(*) >", 5). Insert: $db->table("users")->insert($data). Batch insert: ->insertBatch($rows). Update: ->where("id", $id)->update($data). The Query Builder automatically escapes values, preventing SQL injection. Unlike Eloquent, it does not return objects by default — use getResultArray() for arrays or getResult() for objects.