Viewed   107 times

How can I retrieve the raw executed SQL query in Laravel 3/4 using Laravel Query Builder or Eloquent ORM?

For example, something like this:



(posts (id, user_id, ...))


Otherwise, at the very least how can I save all queries executed to laravel.log?



Laravel 4+

In Laravel 4 and later, you have to call DB::getQueryLog() to get all ran queries.

$queries = DB::getQueryLog();
$last_query = end($queries);

Or you can download a profiler package. I'd recommend barryvdh/laravel-debugbar, which is pretty neat. You can read for instructions on how to install in their repository.

Note for Laravel 5 users: You'll need to call DB::enableQueryLog() before executing the query. Either just above the line that runs the query or inside a middleware.

Laravel 3

In Laravel 3, you can get the last executed query from an Eloquent model calling the static method last_query on the DB class.


This, however, requires that you enable the profiler option in application/config/database.php. Alternatively you could, as @dualed mentioned, enable the profiler option, in application/config/application.php or call DB::profile() to get all queries ran in the current request and their execution time.

Sunday, August 28, 2022

Well I found the solution. It can be done one by passing a closure function in with() as second index of array like

    ->with(array('user' => function($query) {

It will only select id and username from other table. I hope this will help others.

Remember that the primary key (id in this case) needs to be the first param in the $query->select() to actually retrieve the necessary results.*

Tuesday, August 30, 2022

By default, the query log is disabled in Laravel 5:

You will need to enable the query log by calling:


// and then you can get query log


or register an event listener:

    function ($sql, $bindings, $time) {
        //  $sql - select * from `ncv_users` where `ncv_users`.`id` = ? limit 1
        //  $bindings - [5]
        //  $time(in milliseconds) - 0.38 

Some Tips

1. Multiple DB connections

If you have more than one DB connection you must specify which connection to log

To enables query log for my_connection:


To get query log for my_connection:


2. Where to enable query log ?

For an HTTP request lifecycle, you can enable query log in the `handle` method of some `BeforeAnyDbQueryMiddleware` [middleware][1] and then retrieve the executed queries in the [`terminate`][2] method of the same middleware.
class BeforeAnyDbQueryMiddleware
    public function handle($request, Closure $next)
        return $next($request);

    public function terminate($request, $response)
        // Store or dump the log data...

A middleware's chain will not run for artisan commands, so for CLI execution you can enable query log in the artisan.start event listener.

For example you can put it in the bootstrap/app.php file

$app['events']->listen('artisan.start', function(){

3. Memory

Laravel keeps all queries in memory. So in some cases, such as when inserting a large number of rows, or having a long running job with a lot of queries, this can cause the application to use excess memory.

In most cases you will need the query log only for debugging, and if that is the case I would recommend you enable it only for development.

if (App::environment('local')) {
    // The environment is local


Friday, August 12, 2022

From laravel version 5.3^ there is a function to call the app name


You can change the default name 'Laravel' inside this file


'name' => 'Laravel',

And also inside .env file


for latter to work, in config/app.php the line where you set the name should be written like this: 'name' => env('APP_NAME', 'fallback_app_name'),

Friday, November 4, 2022

this worked (ignore the dynamic stuff like this->getClassName etc).. the basic logic works just fine

public function scopeAddTranslations($query)
    $t = new Translation();

    $subq = $t->select('item','text as ref_ar')

    $query->leftjoin(DB::raw('('.$subq->toSql().') as t'), 
      function ($join) use ($subq) { 
                              FROM 14 FOR 26)'),
    return $query;
Sunday, August 7, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :