Home / Learn Secure SQL / Eloquent (Laravel): Query Builder and Raw Bindings

Eloquent (Laravel): Query Builder and Raw Bindings

Fluent, chainable, and parameterised by default.

In shortFluent, chainable, and parameterised by default.

What's actually going on

Eloquent safe patternUser::where('email',$email)->first()Eloquent binds $emailas a parametersingle safe queryexecuted
Eloquent safe pattern

The basic idea

Laravel's Eloquent ORM and underlying Query Builder parameterise values passed to where() and similar methods automatically; raw SQL methods accept a bindings array as a second argument.

Why this comes up

Laravel's fluency (->where('email', $email)) makes the safe path also the shortest path to write, which is exactly the goal of a secure-by-design API.

The mechanics

Use Eloquent/Query Builder methods for everyday queries; for DB::select($sql, $bindings) or whereRaw($sql, $bindings), always pass the bindings array, never a concatenated string.

Where it is used

  • Web application code review
  • Backend development
  • Database administration
Laravel Eloquent
// Safe, idiomatic:
$user = User::where('email', $email)->first();

// Raw SQL, still safe via bindings:
$users = DB::select('SELECT * FROM users WHERE email = ?', [$email]);

Quick check

1. Laravel's DB::select raw method keeps values safe via...