In this tutorial you will learn about the Simple way to Print or Get Last Executed Query in Laravel 8 and its application with practical example.
In this Simple way to Print or Get Last Executed Query in Laravel 8 tutorial I will show you how to print last executed query in laravel application. In this tutorial you will learn to get or print last executed query. In this article I will share various example to print last executed query. I will also show you simple way to get last executed query in laravel. You will also learn to output of print sql query.
Simple way to Print or Get Last Executed Query in Laravel 8
In this step by step tutorial I will demonstrates to get sql query in laravel 8 using toSql(), DB::enableQueryLog() and DB::getQueryLog().
Example 1:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<?php namespace App\Http\Controllers; use App\Models\User; class UserController extends Controller { /** * Create a new controller instance. * * @return void */ public function redirectToGoogle() { $query = User::select("*")->toSql(); dd($query); } } |
Output:-
1 |
select * from `users` |
Example 2:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<?php namespace App\Http\Controllers; use App\Models\User; use DB; class UserController extends Controller { /** * Create a new controller instance. * * @return void */ public function redirectToGoogle() { DB::enableQueryLog(); $users = User::select("*")->get(); $quries = DB::getQueryLog(); dd($quries); } } |
Output:-
1 2 3 4 5 6 7 8 9 10 11 12 13 |
array:1 [▼ 0 => array:3 [▼ "query" => "select * from `users`" "bindings" => [] "time" => 4.25 ] ] |
Example 3:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<?php namespace App\Http\Controllers; use App\Models\User; use DB; class UserController extends Controller { /** * Create a new controller instance. * * @return void */ public function redirectToGoogle() { DB::enableQueryLog(); $users = User::select("*")->get(); $query = DB::getQueryLog(); $query = end($query); dd($query); } } |
Output:-
1 2 3 4 5 |
array:3 [▼ "query" => "select * from `users`" "bindings" => [] "time" => 2.07 ] |