DEV Community

Cover image for SQL to Eloquent Laravel Converter Tool
JJ Labajo
JJ Labajo

Posted on

SQL to Eloquent Laravel Converter Tool

So last week I've been looking for a tool that can convert my raw SQL Statements into a Laravel Eloquent Query Builder but to no avail. So I decided to make one myself.

SQLtoEloquent is an app that lets you convert SQL Statements into Laravel Eloquent Query Builders.

Feel free to use it here:
https://jjlabajo.github.io/SQLtoEloquent/

I've set a couple of rules for accuracy.

Rules:

  1. Use parentheses when using BETWEEN operator. e.g. SELECT * FROM t WHERE (column_name BETWEEN value1 AND value2);
  2. When using ALIAS, always use the AS linking verb. e.g. SELECT uid AS user_id;
  3. Always use backticks (`) for aliases.

Top comments (3)

Collapse
 
saiful00769 profile image
Saiful Islam

not working this query :(

SELECT s1.product_id,s1.totalstock,s2.totalstock as assign,s3.totalstock as warrenty,s4.totalstock as damage,s5.totalstock as missing,s6.totalstock as returnProduct FROM stock_details as s

 left JOIN (SELECT sum(s.quantity) as totalstock,s.product_id FROM stock_details as s WHERE s.status_type=1 GROUP BY s.product_id) as s1 ON s1.product_id = s.product_id left JOIN (SELECT sum(s.quantity) as totalstock,s.product_id FROM stock_details as s WHERE s.status_type=2 GROUP BY s.product_id) as s2 ON s2.product_id = s.product_id left JOIN (SELECT sum(s.quantity) as totalstock,s.product_id FROM stock_details as s WHERE s.status_type=3 GROUP BY s.product_id) as s3 ON s3.product_id = s.product_id left JOIN (SELECT sum(s.quantity) as totalstock,s.product_id FROM stock_details as s WHERE s.status_type=4 GROUP BY s.product_id) as s4 ON s4.product_id = s.product_id left JOIN (SELECT sum(s.quantity) as totalstock,s.product_id FROM stock_details as s WHERE s.status_type=5 GROUP BY s.product_id) as s5 ON s5.product_id = s.product_id left JOIN (SELECT sum(s.quantity) as totalstock,s.product_id FROM stock_details as s WHERE s.status_type=6 GROUP BY s.product_id) as s6 ON s6.product_id = s.product_id GROUP BY s.product_id 
Enter fullscreen mode Exit fullscreen mode
Collapse
 
kingzamzon profile image
Samson Adesanoye

amazing tool..