Laravel 9 How to Get Last Executed Query Tutorial

Reading Time: 5 minutes
1,917 Views

Inside this article we will see the concept of getting last executed query in laravel 9. There are several ways to execute database queries in laravel application like by using models, by using raw queries etc.

There are few super easy methods available by the help of which we can get last executed query from a bulk operation. This tutorial will help you to understand about laravel 9 how to get last executed query in easy steps. Get query logs in laravel 9, this topic will also be covered in this tutorial.

Learn More –

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.

Take an Example to Understand

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

We are 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 we want to print last executed query inside operations we have few methods available.

Let’s see step by step those methods.

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
$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 are logged and printed.

Here, the list of cached query logs will be returned. 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

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 9 How to Get Last Executed Query Tutorial in a very detailed way.

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