Wednesday, March 15, 2017

can't log eloquent (non orm) queries outside laravel

I'm trying to log the query builder queries of eloquent outside laravel with this code. You can see the various var_dump and their results a comment. What would be the proper way to log the queries ?

<?php


use Illuminate\Database\Capsule\Manager as Capsule;


class DbSql
{

    public function db()
    {
        $capsule = new Capsule;
        $capsule->addConnection( [
            'driver'    => Settings::DATABASE_DRIVER,
            'host'      => Settings::DATABASE_HOST,
            'database'  => Settings::DATABASE_NAME,
            'username'  => Settings::DATABASE_USERNAME,
            'password'  => Settings::DATABASE_PASSWORD,
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => ''
        ] );

        # Make this Capsule instance available globally via static methods... (optional)
        $capsule->setAsGlobal();

        # $GLOBALS[ 'debugbar' ][ "messages" ]->addMessage( "Db access" );

        // Enabling query log
        $capsule->connection()->enableQueryLog();

        // Dumps nothing
        $capsule->connection()->listen( function( $query ) {
            var_dump( $query );
        } );

        // Dumps an empty array
        var_dump( $capsule->connection()->getQueryLog() );

        return $capsule;
    }

}



via Robert Brax

Advertisement