Convert Your SQL to Laravel Builder with Orator

Convert Your SQL to Laravel Builder with Orator

Editors Note: The original online tool mentioned below is no longer available and we have replaced the link to point to a cached version.

You can convert raw and legacy SQL queries into a Laravel database query builder version with Orator, an online tool by Maurice Calhoun. This generator could also be an excellent tool for someone learning Laravel, as it can help them translate SQL queries into query builder objects, as learning a new ORM can sometimes be a challenge for new developers.

You simply enter your SQL query, and the generator returns a database query builder. For example, take this SQL query:

select posts.id, posts.title, posts.body from posts
where posts.author_id = 1
order by posts.published_at DESC
limit 10;

The online tool converts it into the following Laravel database query:

DB::select('posts.id','posts.title','posts.body')
    ->from('posts')
    ->where('posts.author_id', '=', 1)
    ->orderBy('posts.published_at', 'DESC')
    ->limit(10)
    ->get();

Try out this SQL to Laravel Query Builder tool online. One thing to note is that you have to replace “backticks” (`) into strings to get it working because the generated builder gives you backticks when it should give you a string. In PHP, the backticks are execution operators.

For those learning how to work with Laravel’s query builder should check out the query builder documentation. We’ve also recently written about optimizing queries with eager loading and quickly dumping Laravel queries which should help you get up to speed on working with databases in Laravel.

Filed in: Laravel
Laravel News Partners

Laravel Jobs

Senior Full-Time Laravel Developer (REMOTE)
Remote, ANYWHERE
Vue School
Senior Laravel Developer
Remote, USA & Canada Only
Givecloud
Backend Software Engineer
Remote, USA Only
CommentSold
Backend PHP Developer
Hamburg
ABOUT YOU GmbH
Senior Developer
Remote
Shelterluv

Newsletter

Join 31,000+ others and never miss out on new tips, tutorials, and more.