ItSolutionStuff.com

How to Get Last Executed Query in Laravel 10?

By Hardik Savani • April 20, 2024
Laravel

Hey Friends,

This definitive guide, we will show you Laravel 10 Eloquent Get Query Log Example. we will help you to give an example of get sql query in laravel 10. This post will give you a simple example of laravel 10 print last sql query. This article will give you a simple example of laravel 10 eloquent print last query. So, let's follow a few steps to create an example of laravel 10 last executed query.

I will print the last SQL query in laravel 10 using toSql(), DB::enableQueryLog(), and DB::getQueryLog(). I will also show you the output of the print SQL query.

So, let's see the examples below and use them as you want anyone.

Example 1:

Controller Code:

<?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:

select * from `users`

Example 2:

Controller Code:

<?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:

array:1 [▼

0 => array:3 [▼

"query" => "select * from `users`"

"bindings" => []

"time" => 4.25

]

]

Example 3:

Controller Code:

<?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:

array:3 [▼

"query" => "select * from `users`"

"bindings" => []

"time" => 2.07

]

I hope it can help you...

Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

Laravel 10 Select2 Ajax Autocomplete Search Example

Read Now →

Laravel 10 Get Client IP Address Example

Read Now →

Laravel 10 Cron Job Task Scheduling Tutorial

Read Now →

Laravel 10 Clear Cache of Route, View, Config, Event Commands

Read Now →

Laravel 10 Send Email using Queue Example

Read Now →

Laravel 10 Change Date Format Examples

Read Now →

Laravel 10 Yajra Datatables Tutorial Example

Read Now →

Laravel 10 REST API Authentication using Sanctum Tutorial

Read Now →

Laravel 10 Ajax Form Validation Example Tutorial

Read Now →

Laravel 10 Mail | Laravel 10 Send Mail Tutorial

Read Now →

Laravel 10 Eloquent Mutators and Accessors Example

Read Now →

Laravel 10 Import Export Excel and CSV File Tutorial

Read Now →

Laravel 10 Authentication using Breeze Tutorial

Read Now →

Laravel 10 CRUD Application Example Tutorial

Read Now →