Skip to content
Readerstacks logo Readerstacks
  • Home
  • Softwares
  • Angular
  • Php
  • Laravel
  • Flutter
Readerstacks logo
Readerstacks
How to print raw sql query in eloquent laravel

How to print raw sql query in eloquent laravel ?

Aman Jain, November 23, 2022March 16, 2024

In this article we will learn to print raw sql query in eloquent laravel. Sometime to debug the MySQL query in laravel eloquent we need to echo raw sql query and want to know exact query. In laravel we have multiple methods to print database query in laravel 8 and we can also print the query before executing it.

Laravel eloquent method to print the query

The first approach to print the MySQL query is laravel eloquent method toSql() which prints the eloquent query without executing it. It returns the raw SQL query however it won’t bind the params as below

<?php 

echo Article::where("title","Test")
->orderBy("id",'DESC')
->limit(5)
->toSql();

Output:

select * from `articles` where `title` = ? order by `id` desc limit 5

Debugging the query using laravel query log

Using laravel query log we can print the entire query with params as well. In this type of debugging query is executed and we will get the complete parsed query.

\DB::enableQueryLog();
 
 Article::where("title","Test")->orderBy("id",'DESC')->limit(5)->get();

dd(\DB::getQueryLog());

Output :

array:1 [▼
  0 => array:3 [▼
    "query" => "select * from `articles` where `title` = ? order by `id` desc limit 5"
    "bindings" => array:1 [▼
      0 => "Test"
    ]
    "time" => 23.71
  ]
]

Here we used DB::enableQueryLog to enable the query log and DB::getQueryLog() to print the all queries in between of it.

Related

Php Laravel debuglaravelphpquery

Post navigation

Previous post
Next post

Related Posts

Php Laravel max in eloquent query

Laravel max in eloquent query with example

January 23, 2022November 7, 2023

In this article we will learn to use aggregate function max in laravel eloquent and query builder. Laravel itself provide inbuilt method to max the columns like in MySQL or SQL based database we can do max of columns using max aggregate function. Laravel eloquent or query builder max method…

Read More
Php How to get random rows in laravel example

How to get random rows in laravel example ?

October 10, 2022March 16, 2024

In this blog post, we’ll take a look at how to get random rows in Laravel database. We’ll explore the use of the QueryBuilder, Eloquent, and the DB facade. laravel has inbuilt eloquent method to get the random records from database using the inRandomOrder method. we will understand the random…

Read More

Enabling and Disabling Debug Mode in Laravel: A Step-by-Step Guide

July 6, 2023March 16, 2024

Debug mode is a useful feature in Laravel that provides detailed error messages and stack traces during development. However, it’s essential to disable debug mode in production to ensure the security of your application. In this blog post, we will walk you through the steps to enable and disable debug…

Read More

Aman Jain
Aman Jain

With years of hands-on experience in the realm of web and mobile development, they have honed their skills in various technologies, including Laravel, PHP CodeIgniter, mobile app development, web app development, Flutter, React, JavaScript, Angular, Devops and so much more. Their proficiency extends to building robust REST APIs, AWS Code scaling, and optimization, ensuring that your applications run seamlessly on the cloud.

Categories

  • Angular
  • CSS
  • Dart
  • Devops
  • Flutter
  • HTML
  • Javascript
  • jQuery
  • Laravel
  • Laravel 10
  • Laravel 11
  • Laravel 9
  • Mysql
  • Php
  • Softwares
  • Ubuntu
  • Uncategorized

Archives

  • May 2025
  • April 2025
  • October 2024
  • July 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • July 2023
  • March 2023
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021

Recent Posts

  • Understanding High Vulnerabilities: A Deep Dive into Recent Security Concerns
  • Understanding High Vulnerabilities in Software: A Week of Insights
  • Blocking Spam Requests with LaraGuard IP: A Comprehensive Guide
  • Enhancing API Development with Laravel API Kit
  • Exploring the Future of Web Development: Insights from Milana Cap
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version