laravel development

How to Convert Your SQL to Laravel Query Builder

You think of something and someone out there will already be workig on it. Well not always but yes most of the time.

If you are someone learning Laravel a great help is provided by Orator an online tool which converts the legacy SQL queries into Laravel database query builder version. As a learner if you can get SQL queries translated into query builder objects it is a boon as learning a new ORM is generally a challenge for most of the new developers.

What you need to do is simply enter your SQL query, and the generator returns a database query builder.

For example, take this SQL query:
       select, posts.title, posts.body from posts
       where posts.author_id = 1
      order by posts.published_at DESC
      limit 10;

The tool converts it into Laravel database query:

     ->where(‘posts.author_id’, ‘=’, 1)
     ->orderBy(‘posts.published_at’, ‘DESC’)

One thing that you should keep in mind is to replace “backticks” (`) into strings to get it working because the generated builder gives you backticks when it should give you a string.

Try out this SQL to Laravel Query Builder tool on the web and explore the possibilities with Laravel.