laravel get query – How to display query log in Laravel?

laravel get query Using ->toSql(), ->get(), getQueryLog() methods. You can use DB::getQueryLog() function to get all executed queries if you want to get last executed then use end() method.

laravel get query

The simplest method to see the query generated is by utilizing a ->toSql() method. The first method to get the query of an Eloquent call is by using the toSql() method.

Read Also:  Eloquent Order By Relation Column in Laravel

Example 1: Controller Code

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

Example 2: Controller Code

DB::enableQueryLog();
$Members = Member::select("*")->get();
$quries = DB::getQueryLog();
dd($quries);

Example 3: Controller Code

DB::enableQueryLog();
  
$Members = Member::select("*")->get();
$query = DB::getQueryLog();
$query = end($query);
dd($query);

Don’t Miss : Laravel Get All Query String

Laravel 8 Eloquent Get Query Log Example

Example

<?php
namespace App\Http\Controllers;

use App\Models\Member;
use DB;

class MemberController extends Controller
{

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function redirectToGoogle()
    {
        DB::enableQueryLog();
        $Members = Member::select("*")->get();
        $query = DB::getQueryLog();
        $query = end($query);
        dd($query);

    }

}

I hope you get an idea about laravel get query.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.