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 How to Create Rest APIs in Laravel

How to Create Rest APIs in Laravel ?

July 4, 2022August 2, 2022

Rest APIs are used to create the communication between client and server and in this application we will learn to create rest apis in laravel. Rest API is a architectural style which same as both server and client end so any client can consume the apis since it has industry…

Read More
Php How to Call a POST Rest Api in Laravel

How to Call a POST Rest Api in Laravel ?

June 7, 2022June 7, 2022

In this article we will learn to call an post rest api in Laravel. Whenever we want to access the third party data we need to access the data using the APIs, we send a request to another server means outside our application and they respond with preformatted structure. Post…

Read More
Php How to create multiple size thumbs from image in laravel

How to create multiple size thumbs from image in laravel 8 ?

May 14, 2022February 22, 2024

Resizing images can be a best performance booster for any application since this will load the proper size images. In this article i will show you to create multiple size thumbs from image in laravel using intervention package. Big size of image can reduce the performance of the application therefor…

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

  • June 2025
  • 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

  • The Resilience of Nature: How Forests Recover After Fires
  • Understanding Laravel Cookie Consent for GDPR Compliance
  • Understanding High Vulnerabilities: A Critical Overview of the Week of May 12, 2025
  • Installing a LAMP Stack on Ubuntu: A Comprehensive Guide
  • Understanding High Vulnerabilities: A Deep Dive into Recent Security Concerns
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version