Readytocode
  • Home
  • Laravel
  • PHP
  • React Js
  • Git
  • Server
Facebook X (Twitter) Instagram
Readytocode
  • Home
  • Laravel
  • PHP
  • React Js
  • Git
  • Server
Facebook X (Twitter) Instagram Pinterest LinkedIn Reddit
Readytocode
You are at:Home How To Enable Query Log In Laravel?

Laravel

How To Enable Query Log In Laravel?

JustinBy JustinJanuary 31, 2022Updated:January 31, 2022No Comments2 Mins Read
Facebook Twitter Pinterest LinkedIn Tumblr Email




In this article, I show you both options of printing and logging queries that help to view the queries in your Laravel application.

While developing a projects, sometimes we may come across a situation where you need to see if the written query is correct or not.

if you are logging the queries then it will help to debug the issue. Let’s see first printing queries in Laravel.

How to Print Query in Laravel?

<?php
namespace App\Http\Controllers\API;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Student;
class StudentController extends Controller
{
   public function list(){
   DB::enableQueryLog();
   $students=Student::all();
   dd(DB::getQueryLog());
  }
}

 

 

Now, if i run the code i will have the select query printed. Using the above technique, you can also print the queries executed through Eloquent.

Log Query in Laravel

It is a more easy and right way for me. Only need to add some code in the AppServiceProvider.php and done.

Open the AppServiceProvider.php file, and add 2 Facades for ‘File’ and ‘DB’ as below lines.

use Illuminate\Support\Facades\File;

use Illuminate\Support\Facades\DB;

Next, in the boot() method write the below code that will listen to each SQL query executed and log them in the storage/logs/query.log.

public function boot() {
DB::listen(function($query) {
File::append( storage_path('/logs/query.log'), $query->sql . ' [' . implode(', ', $query->bindings) . ']' . PHP_EOL );
});
}

 

One more way is available to log the queries. Using this way your query logs get stored inside the storage/logs/laravel.log. For this, you need to add the below code to your routes/web.php file. 

\DB::listen(function($sql) {
\Log::info($sql->sql);
\Log::info($sql->bindings);
\Log::info($sql->time);
});

 

Conclusion

you can choose either one of the above methods for logging the queries in Laravel.
Total
0
Shares
Share 0
Tweet 0
Pin it 0
Share 0

Like this:

Like Loading...

Related

Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
Justin
  • Website

Related Posts

How to Generate image from text with Laravel 10

May 16, 2023

Laravel 10 Restrict User Access

May 3, 2023

Laravel 10 Pagination Example

May 3, 2023

Leave A Reply Cancel Reply

Featured Posts
  • Why meta tag is important for seo 1
    How to change react Page title, Page Description and some other meta Tag
    • August 4, 2023
  • 2
    How to Generate image from text with Laravel 10
    • May 16, 2023
  • Laravel 10 Restrict User Access 3
    Laravel 10 Restrict User Access
    • May 3, 2023
  • Laravel 10 Pagination Example 4
    Laravel 10 Pagination Example
    • May 3, 2023
  • install Steam on Ubuntu 5
    How to install Steam on Ubuntu 22.04 step by step
    • April 20, 2023



Readytocode
Facebook X (Twitter) Instagram Pinterest
  • About Us
  • Privacy Policy
© 2025 ReadyToCode.

Type above and press Enter to search. Press Esc to cancel.

%d