xxxxxxxxxx
check whethe the serviceprovider is added in the providers array in config/app.php
if no then check whether the service provider class is the correct location and
include the serivce provider in the providers array in config.app.php
xxxxxxxxxx
DB::enableQueryLog();
$arr_user = DB::table('users')->select('name', 'email as user_email')->get();
dd(DB::getQueryLog());
xxxxxxxxxx
DB::connection()->enableQueryLog(); //enable query log
$data = $order->all(); //query execute
$queries = DB::getQueryLog(); //get query
return dd($queries); //show query
xxxxxxxxxx
use Illuminate\Support\Facades\DB;
public function UserController()
{
DB::enableQueryLog();
$arr_user = DB::table('users')->select('name', 'email as user_email')->get();
dd(DB::getQueryLog());
}
xxxxxxxxxx
DB::enableQueryLog();
$user = DB::table('users')->select('name', 'email as user_email')->get();
dd(DB::getQueryLog());
xxxxxxxxxx
//AppServiceProvider add boot function
if (config('app.debug')) {
DB::connection()->enableQueryLog();
}
//function formatSize
public static function formatSize(int $bytes): string
{
if ($bytes < 1000 * 1024) {
return number_format($bytes / 1024, 2).' KB';
}
if ($bytes < 1000 * 1048576) {
return number_format($bytes / 1048576, 2).' MB';
}
if ($bytes < 1000 * 1073741824) {
return number_format($bytes / 1073741824, 2).' GB';
}
return number_format($bytes / 1099511627776, 2).' TB';
}
//debug.blade.php
@php
/** @var \App\Models\User $user */
$user = auth()->user();
$queries = ($user->id === 1 && auth()->check()) ? Illuminate\Support\Facades\DB::getQueryLog() : [];
@endphp
@if(count($queries) > 0)
<div class="btn" data-bs-toggle="modal" data-bs-target="#sqlQueryLogModal">
<a href="#" data-toggle="modal" data-target=".query_log_modal">SQL: <?= count($queries) ?></a>,
{{ \App\Services\AdminService::formatSize(memory_get_usage()) }} {{ round(microtime(true) - LARAVEL_START, 2) }} sec.
</div>
@if(!empty($queries))
<div class="modal fade" id="sqlQueryLogModal" tabindex="-1" aria-labelledby="sqlQueryLogModalLabel" aria-hidden="true">
<div class="modal-dialog modal-xl" role="document">
<div class="modal-content">
<div class="modal-header align-items-center">
<h4 class="modal-title">System Sql query log</h4>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body p-2">
<table class="table table-sm table-bordered">
<thead>
<tr class="bg-light">
<th scope="col" class="border-top-0">#</th>
<th scope="col" class="border-top-0">Query</th>
<th scope="col" class="border-top-0 text-center">Bindings</th>
<th scope="col" class="border-top-0 text-center">Time</th>
</tr>
</thead>
<tbody>
@foreach($queries as $k => $query)
<tr>
<th scope="row">{{ $k + 1 }}</th>
<td style=" text-align: left; ">
<code style="white-space: pre-wrap; word-wrap: break-word; margin: 0; ">{{ $query['query'] }}</code>
</td>
<td class="text-center">
@if(!empty($query['bindings']))
@foreach($query['bindings'] as $binding)
<span class="badge bg-secondary">{{ $binding }}</span>
@endforeach
@endif
</td>
<td class="text-center"><span class="badge bg-secondary">{{ $query['time'] }} ms</span></td>
</tr>
@endforeach
<tr>
<th scope="row" colspan="3">Total time</th>
<td><span class="badge bg-secondary">{{ round(microtime(true) - LARAVEL_START, 2) }} sec.</span></td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
@endif
@endif
xxxxxxxxxx
Method #1
instead of ->get use ->toSql() on query
$users = User::orderBy('name', 'asc')->toSql();
echo $users;
// Outputs the string:
'select * from `users` order by `name` asc'
Method # 2
DB::enableQueryLog();
// and then you can get query log
dd(DB::getQueryLog());
xxxxxxxxxx
namespace App\Providers;
use DB;
use Log;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
DB::listen(function($query) {
Log::info(
$query->sql,
$query->bindings,
$query->time
);
});
}
// ...
}
xxxxxxxxxx
DB::listen(function ($query) {
var_dump([
$query->sql,
$query->bindings,
$query->time
]);
});
xxxxxxxxxx
DB::enableQueryLog();
$arr_user = DB::table('users')->select('name', 'email as user_email')->get();
dd(DB::getQueryLog());