Laravel 8 How to Get Last Executed Query
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 –
- How To Upload And Save XML Data in Laravel 8
- How To Use Factory in Seeder Laravel 8 Tutorial
- How To Use Laravel 8 Flash Message with Bootstrap Tutorial
- How To Work with Session Timeout in Laravel 8
Let’s get started.
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.