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 Laravel ajax login and register

Laravel ajax login and registration with example

December 4, 2021November 12, 2023

In Laravel 8 there is multiple ways to implement the laravel Ajax login and registration like Laravel provides its own auth with packages Jetstream, passport,sanctum, breeze and fortify. These all packages are easy to install and configure but sometimes our application requirement and design patterns are different or we can…

Read More
Php Use Soft Delete to Temporary (Trash) Delete the Records in Laravel

Use Soft Delete to Temporary (Trash) Delete the Records in Laravel ?

June 17, 2022June 17, 2022

In laravel we use Soft Delete to Temporary (Trash) Delete the Records. Soft delete in laravel is use to hide the record from users by keeping the data in the database. There is many purpose of soft delete like deleting the user for admin but keep all the information in…

Read More
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

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