Laravel 8 How to Get Last Executed Query

Reading Time: 5 minutes
5,736 Views

Inside this article we will see the concept of getting last executed query in laravel 8. 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 8 how to get last executes query in easy steps. Get query logs in laravel 8, this topic will also be covered in this tutorial.

Learn More –

Let’s get started.


Laravel Installation

We will create laravel project using composer. So, please make sure your system should have composer installed. If not, may be this article will help you to Install composer in system.

Here is the command to create a laravel project-

composer create-project --prefer-dist laravel/laravel blog

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 several 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.

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.

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

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.