Field Manual PHP / Laravel reference
← Laravel

Query Builder

The query builder builds parameterized SQL without full models.

What it is

The query builder builds parameterized SQL without full models.

When to use it

Use for reporting, bulk operations, or selected columns.

Syntax

DB::table("orders")->where("status", "paid")->get();

Example

use Illuminate\Support\Facades\DB;
$totals = DB::table("orders")
    ->select("user_id")
    ->selectRaw("SUM(total_cents) AS total_cents")
    ->where("status", "paid")
    ->groupBy("user_id")
    ->get();

Common mistakes

Raw expressions must be constant or bound; whitelist dynamic column names.

Related topics

Browse Laravel