Logging to `php artisan serve` output

laravel, php

Solution

I need to be more patient with Google. I found a clue in another question and after some tweaking, I got it to behave just the way I want.

Throw this into `app/start/global.php`:

DB::listen(function($sql, $bindings, $time)
{
    file_put_contents('php://stdout', "[SQL] {$sql} \n" .
                      "      bindings:\t".json_encode($bindings)."\n".
                      "      time:\t{$time} milliseconds\n");
});

And `php artisan serve` will output SQL queries to the dev server console.

Problem

Is there a way for your Laravel application to log output to the standard output of the PHP development server that `php artisan serve` uses? In this particular case, I want to output (Eloquent-generated) SQL queries.

Original source