Last updated: 24 November 2026. I could not run the converter itself from my environment, so the conversions below are hand-written equivalents, not tool output. I did check them by running the code through Laravel's real MySQL query grammar. SeeHow this was checked.
You have a working SQL query, from a report, a legacy script or a phpMyAdmin session, and you need it as Laravel code. Doing it by hand is tedious and easy to get subtly wrong, especially around grouped OR conditions. The SQL to Laravel Query Builder Converter on Dev Talk does the first draft in your browser. This article covers what it does, how to use it, and how to confirm the generated code runs the SQL you started with.
What the tool does
You paste a MySQL or MariaDB query and the tool produces Laravel code, either a DB::table() query builder chain or an Eloquent Model:: chain. The page lists support for joins, grouped where conditions, whereIn, whereBetween, subqueries, GROUP BY, HAVING and LIMIT, plus aggregates and INSERT, UPDATE and DELETE statements. The page does not say whether CTEs (WITH) or UNION are supported, so test those before relying on them.
What you get on the page:
- A text area labelled "SQL (MySQL / MariaDB)". Separate multiple statements with semicolons.
- An examples dropdown and a Clear button.
- A switch between Query builder (
DB::table) and Eloquent (Model::) output, with code that updates as you type. - A "Copy .php" button.
- Notes under the code that flag raw expressions, placeholders turned into variables, and models you need to check.
The page says conversion happens in your browser and your SQL is never uploaded. I could not verify that independently. If your query contains real customer data, replace the values with placeholders first.
How placeholders are handled
According to the page, each ? becomes $param1, $param2 and so on, and each :name becomes $name. Values become bindings, as in where('email', $email), which Laravel sends separately from the SQL. You must replace the placeholder variables with your own values before the code will run. Anything the builder has no method for becomes DB::raw() or whereRaw(), and those are the lines to read twice.
An example conversion
The page's own example queries are only available from its dropdown, so here is a hand-written one. The SQL:
SELECT orders.id, users.email, orders.total
FROM orders
INNER JOIN users ON users.id = orders.user_id
WHERE orders.status = :status
AND (orders.total > 100 OR users.vip = 1)
AND orders.region IN ('EU', 'US')
AND orders.created_at BETWEEN :from AND :to
ORDER BY orders.id DESC
LIMIT 20;
As query builder code, this is what a correct conversion looks like:
<?php
use Illuminate\Support\Facades\DB;
$rows = DB::table('orders')
->join('users', 'users.id', '=', 'orders.user_id')
->select('orders.id', 'users.email', 'orders.total')
->where('orders.status', $status)
->where(function ($query) {
$query->where('orders.total', '>', 100)
->orWhere('users.vip', 1);
})
->whereIn('orders.region', ['EU', 'US'])
->whereBetween('orders.created_at', [$from, $to])
->orderByDesc('orders.id')
->limit(20)
->get();
I ran that chain through Laravel's MySQL grammar and got this SQL, with the bindings listed after it:
select `orders`.`id`, `users`.`email`, `orders`.`total` from `orders` inner join `users` on `users`.`id` = `orders`.`user_id` where `orders`.`status` = ? and (`orders`.`total` > ? or `users`.`vip` = ?) and `orders`.`region` in (?, ?) and `orders`.`created_at` between ? and ? order by `orders`.`id` desc limit 20
["paid",100,1,"EU","US","2026-01-01","2026-03-31"]
That matches the original query: same join, same grouping, same filters. Two details are worth noticing. The status and date range values became bindings, while the limit value is written into the SQL directly. And the OR condition is wrapped in parentheses, which is the next section's subject.
Why the tool wraps OR conditions in closures
The page says grouped AND/OR conditions become closures, and explains that without one, orWhere() applies to the whole query. I confirmed the difference. These two chains look similar:
-- no closure
select * from `posts` where `author_id` = ? and `status` = ? or `featured` = ?
-- with closure
select * from `posts` where `author_id` = ? and (`status` = ? or `featured` = ?)
The first returns every featured post regardless of author, because SQL evaluates AND before OR. The second returns only posts by that author that are live or featured. If you ever hand-edit generated code and remove a closure, this is the bug you create.
Aggregates, grouping and raw expressions
Aggregates such as COUNT(*) and SUM(total) have no dedicated select method in a grouped query, so they come out as raw expressions. That is normal. The mistake to avoid is putting user input inside one:
<?php
use Illuminate\Support\Facades\DB;
$rows = DB::table('orders')
->select('user_id', DB::raw('COUNT(*) as order_count'), DB::raw('SUM(total) as revenue'))
->groupBy('user_id')
->having('revenue', '>', 1000)
->orderByDesc('revenue')
->get();
// User input in a raw expression: pass it through bindings, never concatenate it.
$match = DB::table('users')
->whereRaw('LOWER(email) = ?', [strtolower($email)])
->first();
The grouped query above ran through the grammar as select user_id, COUNT(*) as order_count, SUM(total) as revenue from orders group by user_id having revenue > ? order by revenue desc with [1000] as the binding. The raw expression is fine because it contains no user input. In the second example, the user value goes through the bindings array, not into the string. The tool's page gives the same advice: keep user input out of raw expressions or pass it through bindings.
The page also says values become bindings, so the generated output is safe from SQL injection. That holds for bound values. It does not cover raw expressions you build with string concatenation. For background, see the related article on SQL injection prevention linked below.
Query builder or Eloquent?
The page's summary is that Eloquent returns models with relationships, casts and events, while the query builder returns plain objects and is slightly faster for reports. The practical rule: use DB::table() for reporting queries that join several tables and return rows you only display, and use Eloquent when you need model behavior. The tool's Eloquent output needs a real model class, so check the model names it chooses against your app, because table names do not always map to the class you expect. The notes under the code flag models to check.
An Eloquent version of the first query looks like this, with a quick way to look at the SQL before you trust it:
<?php
use App\Models\Order;
$query = Order::query()
->join('users', 'users.id', '=', 'orders.user_id')
->select('orders.id', 'users.email', 'orders.total')
->where('orders.status', $status)
->limit(20);
// Check what will run before you trust it.
$sql = $query->toSql(); // SQL with ? placeholders
$bindings = $query->getBindings();
$query->dump(); // prints SQL and bindings, then continues
toSql(), getBindings() and dump() are available on an Eloquent builder because it passes them through to the underlying query builder; I confirmed this in the framework source. toRawSql() exists on the query builder too, and substitutes the bindings into the SQL for easy comparison, but never log or share its output when it contains sensitive values.
How to verify a conversion
- Paste the SQL, copy the generated code, and replace
$param1-style variables with real values. - Call
toSql()andgetBindings(), ortoRawSql()in a development environment, and compare the output with your original query clause by clause. - Run both against the same database and compare the row counts on a realistic dataset.
- Read every
DB::raw()andwhereRaw()line separately, since those bypass the builder's escaping.
Limits to expect
- The tool targets MySQL and MariaDB syntax. Standard SQL from PostgreSQL or SQLite often works as long as it avoids database-specific functions, according to the page.
- Database-specific functions end up in raw expressions, which you then need to check on your database.
- Complex queries (window functions, recursive CTEs, vendor hints) may not convert cleanly. The page does not list them as supported, so treat the output as a starting point.
- The tool needs JavaScript.
Related Dev Talk articles
- SQL Injection in PHP: PDO and MySQLi Prevention
- cURL to Laravel HTTP Client: Convert curl Commands to Http:: Code
- Laravel Soft Deletes Pitfalls
Frequently asked questions
How do I convert a SQL query to Laravel query builder code?
Paste the query into the converter, choose Query builder or Eloquent output, copy the code, and replace the placeholder variables with your own values. Then compare toSql() with the original query.
Is the converted code safe from SQL injection?
Values are turned into bindings, so those are safe. Raw expressions are the exception, so keep user input out of them or pass it through the bindings array.
What happens to ? and :name placeholders?
According to the tool's page, ? becomes $param1, $param2 and so on, and :name becomes $name.
Why are my OR conditions wrapped in a closure?
A closure produces parentheses in the SQL. Without it, an orWhere() applies to the whole query and can return the wrong rows.
Does it support PostgreSQL?
The tool targets MySQL and MariaDB. The page says PostgreSQL or SQLite queries usually work if they avoid database-specific functions.
Is my SQL uploaded to a server?
The page says it is converted entirely in your browser and never uploaded. Even so, avoid pasting real customer data.
update log
- 24 November 2026: first version.
- To recheck on each update: the tool's supported clauses and options, real converter output to replace my hand-written examples, and Laravel's query builder behavior.
Sources
- Dev Talk: SQL to Laravel Query Builder Converter
- Laravel documentation: Database Query Builder
- laravel/framework: Query/Builder.php
Originally published on DEV Talk.
Top comments (0)