Skip to content
Readerstacks logo Readerstacks
  • Home
  • Softwares
  • Angular
  • Php
  • Laravel
  • Flutter
Readerstacks logo
Readerstacks

How to print database query in laravel 8 ?

Aman Jain, January 1, 2022February 22, 2024

Sometime to debug the MySQL query in laravel eloquent we need to print the raw MySQL 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 Show Success and Error Flash Message in Laravel

How to Show Success and Error Flash Message in Laravel ?

June 27, 2022November 7, 2023

In this article we will learn to show flash success and error message in laravel. Flash messages are useful when we want to notify the user about their recent activity like submit a form or making any action on website so in this case it may multiple type of messages…

Read More
Php How to Move Files or Folder to One Folder to Another in Laravel

How to Move Files or Folder to One Folder to Another in Laravel ?

May 27, 2022

Move command is used to shift the folder or files from one location to another without copy so In this article i will show you move files or folder to one folder to another in Laravel. File System is used to perform the file system based operations like move files…

Read More
Php How to Implement Remember Me Feature in Laravel

How to Implement Remember Me Feature in Laravel ?

August 12, 2022March 16, 2024

Laravel authentication offers remember me functionality out of the box and to implement remember me feature in laravel we only need to follow some guidelines provided by laravel so in this article i will show you the simplest way to implement this. Most of the time while developing login functionality of application…

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

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

  • Mastering Schedule Management with Laravel Zap
  • 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
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version