Laravel 10 How to Get Last Executed Query Tutorial

Reading Time: 5 minutes
259 Views

Accessing the most recently run SQL query in Laravel 10 is a useful tool for debugging and optimising database interactions in your application. The ability to get the most recently executed query gives developers insight into the actual SQL statement submitted to the database, assisting in the identification of potential errors and fine-tuning the application’s performance.

In this tutorial, we will walk you through the process of obtaining the most recently run query in Laravel 10. We’ll look at Laravel’s query logging functionality and show you how to obtain the most recently executed query programmatically.

Read More: Beta Character AI: A Complete Guide For Character.ai

Let’s get started.

Laravel Installation

Open terminal and run this command to create a laravel project.

composer create-project laravel/laravel myblog

It will create a project folder with name myblog inside your local system.

To start the development server of laravel –

php artisan serve

URL: http://127.0.0.1:8000

Assuming laravel already installed inside your system.

Consider an example (Get Last Run Query)

Suppose we have few models like Order.php and Country.php which are associated with orders and countries table.

Here, running few queries inside controller’s method.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Country;
use App\Models\Order;

class SiteController extends Controller
{
    public function index()
    {
        // Query 1
        $orders = Order::where("id", 45);
      
        // Query 2
        $countries = Country::all();

      	// Query 3
        $queryx = Order::select("*")->get();
      
        //...
    }
}

If you want to print last executed query inside operations. Here, is the way to do that.

Method #1: Using toSql() Method

toSql() method will return executed query in mysql statement. It is straightforward to get the current SQL query you can do it with Laravel query builder’s toSql() method.

Concept to get last executed query.

Usage

$query = Order::select("*")->toSql();
dd($query);

//Output
select * from `orders

Read More: Laravel 10 How To Add Social Media Share Buttons Tutorial

$query = Order::where("id", 55)->toSql();
dd($query);

//Output
select * from `orders` where `id` = ?

Method #2: Using Query Log of Laravel

To enable the query log by taking the help of Laravel query builder’s DB::enableQueryLog() method.

The enableQueryLog() method stores all the executed queries in the cache that we can easily access with DB::getQueryLog() method.

Get all Executed Queries

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use DB;
use App\Models\Country;
use App\Models\Order;

class SiteController extends Controller
{
    public function index()
    {
        DB::enableQueryLog(); // to enable query log

        $specific_order = Order::where("id", 55)->get();
        $countries = Country::all();

        $all_orders = Order::select("*")->get();

        $query = DB::getQueryLog(); // get query logs from cache
        
        dd($query);
 
    }
}

Output returns in array format. All cached queries will be logged and printed.

Here, the list of cached query logs. It is in array format. To get last element from this array we can use end() function. It’s a php function.

Get Last Executed Query

The end() is a PHP function. This function returns the last element from an array.

DB::enableQueryLog();

$specific_order = Order::where("id", 55)->get();
$countries = Country::all();

$all_orders = Order::select("*")->get();

$query = DB::getQueryLog();

$last_query = end($query);

dd($last_query);

We hope this article helped you to learn Laravel 10 How to Get Last Executed Query Tutorial in a very detailed way.

Read More: Unblocked Games Premium: Play Unblocked Free Games

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.

Sanjay KumarHello friends, I am Sanjay Kumar a Web Developer by profession. Additionally I'm also a Blogger, Youtuber by Passion. I founded Online Web Tutor and Skillshike platforms. By using these platforms I am sharing the valuable knowledge of Programming, Tips and Tricks, Programming Standards and more what I have with you all. Read more