Tools / SQL to Laravel Query Builder

SQL to Laravel Query Builder.

Convert a SELECT statement into Laravel Query Builder and Eloquent code.

Runs in your browser

Ctrl/⌘ + Enter to run · Ctrl/⌘ + Shift + C to copy the result

Result
Query Builder
Eloquent
Notes

About SQL to Laravel Query Builder

SQL to Laravel Query Builder rewrites a SELECT statement as fluent Laravel code. Paste the query you tested in your database client and get two versions: one that starts with DB::table() and one that starts from an Eloquent model inferred from the table name, so orders becomes Order::query(). The converter maps each part of the statement to the method Laravel expects: plain columns go to select(), expressions such as COUNT(o.id) go to selectRaw(), INNER, LEFT, RIGHT and CROSS joins become join(), leftJoin(), rightJoin() and crossJoin(), and multi-condition joins use a closure with on() and where(). In WHERE, comparisons become where(), IN lists become whereIn(), BETWEEN becomes whereBetween(), IS NULL becomes whereNull(), and column-to-column comparisons become whereColumn(). Parenthesised OR groups turn into nested where(function ($query) { … }) closures, which is the part people most often get wrong by hand: writing orWhere() at the top level silently changes the meaning of the query. GROUP BY, HAVING, ORDER BY, LIMIT and OFFSET follow, and a LIMIT 1 query ends with first() instead of get(). Placeholders such as ? and :email become PHP variables so you remember to bind them. Anything the converter cannot express safely, like functions in WHERE, is kept as a whereRaw() fragment and flagged in the notes. Sub-queries and UNION are not supported yet. The conversion runs entirely in your browser and the query is never sent anywhere.

How to Use

  1. Paste a single SELECT statement; a trailing semicolon is fine.
  2. Read the Query Builder and Eloquent versions side by side and check the notes for raw fragments.
  3. Copy the chain into your controller or repository and bind any placeholder variables.

Frequently Asked Questions

Why is my OR condition wrapped in a closure?

SQL gives AND a higher priority than OR. A top-level orWhere() would apply to the whole query, so grouped conditions are wrapped in where(function ($query) { … }) to keep the parentheses from your SQL.

How is the Eloquent model name chosen?

It is the singular StudlyCase form of the table name, the same convention Laravel uses, so order_items becomes OrderItem. If your model uses a different table, adjust the class name.

What does it not convert?

Sub-queries, UNION, window functions and FULL OUTER JOIN. Functions inside WHERE or HAVING are kept as raw SQL fragments and listed in the notes.

Is the query uploaded?

No. Parsing runs in your browser, and nothing is sent to a server.

Related Tools