laravel sql query log code example

Example 1: How to Log Query in Laravel

DB::enableQueryLog();
$arr_user = DB::table('users')->select('name', 'email as user_email')->get();
dd(DB::getQueryLog());

Example 2: How to Log Query in Laravel

DB::enableQueryLog();
$user = DB::table('users')->select('name', 'email as user_email')->get();
dd(DB::getQueryLog());

Example 3: laravel db query log string replacements

DB::enableQueryLog();

# <!-- YOUR QUERY HERE --->
User::all();

$query = collect(DB::getQueryLog())->map(function($query){
  return str_replace_array('?', $query['bindings'], $query['query']);
})->join("\n");

dd($query); // Outputs: select * from `users`

Tags:

Php Example