How to print sql queries to Log file (in Laravel)

This is just a quick Laravel snippet on how to log SQL to the log file. You just need to enable logging for DB , run SQL ( and then turn off or leave on its up to you).

DB::enableQueryLog();
        
$results = DB::select( $someSql ) ;

Log::debug(DB::getQueryLog());

DB::disableQueryLog();

Oh and remember to include the classes you’re using (usually at the top of your file).

use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\DB;

Leave a Comment