xxxxxxxxxx
DB::enableQueryLog();
$arr_user = DB::table('users')->select('name', 'email as user_email')->get();
dd(DB::getQueryLog());
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
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());
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
public function show(Order $order){
\DB::connection()->enableQueryLog();
$data = $order->all();
$queries = \DB::getQueryLog();
return dd($queries);
}