Member-only story
Log Your Laravel Database Query Like A Pro

When you developed Laravel application, sometime everything look like normal and ok in your development stage, but usually your application will be slower after your data grow up, let say your data in hundreds of thousands data, if your query was not good or optimize your application will be slower than before.
But i think it’s normal if you are in junior level, and after a long time your experience grow, you will know when you are doing code if this query isn’t good 😁
Ok let’s to the topic, based on my experience for log database query it have 2 method, first one use laravel pulse (https://pulse.laravel.com/) and the last one made query log manually and placing in app service provider.
1 Log From AppServiceProvider.php
Let’s start from basic, for this step you just put this code into your AppServiceProvider.php
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Log;
use DB;
class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*/
public function register(): void
{
//
}
/**
* Bootstrap any application services.
*/
public function boot(): void
{
// 1 seconds
$queryLimitInSeconds = 1000;
// Enable…