Laravel 8 How to Get Last Executed Query

Share this Article
Reading Time: 5 minutes
383 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.


Installation of Laravel Application

Laravel Installation can be done in two ways.

  • Laravel Installer
  • By using composer

Laravel Installer

To install Laravel via Laravel installer, we need to install it’s installer first. We need to make use of composer for that.

$ composer global require laravel/installer

This command will install laravel installer at system. This installation is at global scope, so you type command from any directory at terminal. To verify type the given command –

$ laravel

This command will open a command palette of Laravel Installer.

To create ad install laravel project in system,

$ laravel new blog

With the name of blog a laravel project will be created at your specified path.

By using composer

Alternatively, we can also install Laravel by Composer command create-project. If your system doesn’t has Composer Installed, Learn Composer Installation Steps. Here is the complete command to create a laravel project-

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

After following these steps we can install a Laravel application into system.

To start the development server of Laravel –

$ php artisan serve

This command outputs –

Starting Laravel development server: http://127.0.0.1:8000

Assuming laravel already installed at 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 executes 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.

Find More on Laravel 8 Articles here