Laravel 5.5 – How to log all Eloquent Queries


In this post, I will show you simple method to log each and every Eloquent Query of your application executes. We gonna log queries to the storage/logs/laravel.log file.

To log database queries we gonna add a database calls lister as shown in the below example. Open your app/Providers/AppServiceProvider.php and add following code to boot() method:


namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Schema;
use DB;
use Log;

class AppServiceProvider extends ServiceProvider
{
    public function boot()
    {
        Schema::defaultStringLength(191);

        DB::listen(function($query) {
            Log::info(
                $query->sql,
                $query->bindings,
                $query->time
            );
        });
    }

Example log data

[2017-12-04 18:57:33] local.INFO: select * from `tasks`  
[2017-12-04 18:57:33] local.INFO: select count(*) as aggregate from `tasks`  
[2017-12-04 18:57:33] local.INFO: select * from `tasks` limit 15 offset 0  
[2017-12-04 18:57:33] local.INFO: select * from `tasks`  
[2017-12-04 18:57:33] local.INFO: select count(*) as aggregate from `tasks`  
[2017-12-04 18:57:33] local.INFO: select * from `tasks` limit 15 offset 0  
I hope you like this Post, Please feel free to comment below, your suggestion and problems if you face - we are here to solve your problems.

Search In multidimensional array by keys value

// Array representing a possible record set returned from a database
$records = array(
    array(
        'id' => 2135,
        'first_name' => 'John',
        'last_name' => 'Doe',
    ),
    array(
        'id' => 3245,
        'first_name' => 'Sally',
        'last_name' => 'Smith',
    ),
    array(
        'id' => 5342,
        'first_name' => 'Jane',
        'last_name' => 'Jones',
    ),
    array(
        'id' => 5623,
        'first_name' => 'Peter',
        'last_name' => 'Doe',
    )
);
$id = array_search(5623, array_column($records, 'id'));
print_r($records[$id]);

Reference :

1) https://stackoverflow.com/questions/6661530/php-multidimensional-array-search-by-value
2) https://vijayasankarn.wordpress.com/2017/02/20/array_unique-for-multidimensional-array/

read and write into file using Piping in NodeJs

var fs = require('fs');

var stream = fs.createReadStream('data.json'),
writable = fs.createWriteStream('data_copy.json');

stream.pipe(process.stdout);

stream.pipe(writable);

Reading txt File in chunk in nodeJs using stream (updated : added pause and resume)

var fs = require('fs');

var stream = fs.createReadStream('data.json');


//stream.pause(); To pause the streaming
//stream.resume(); To resume the streaming


stream.on('data', function (chunk) {


console.log('-----------------START----------------');
console.log(chunk.toString());
console.log('----------------FINISH----------------');

});


stream.on('data', function (chunk) {

console.log('CHUNK LENGTH WAS: ' + chunk.length);

});

stream.on('end', function () {

console.log('----------------END OF THE FILE----------------');

});

Create getter and setter module in NodeJS

var number, origin, destination;

exports.setNumber = function (num) {
number = num;
};

exports.setOrigin = function (o) {
origin = o;
};

exports.setDestination = function (d) {
destination = d;
};

exports.getInfo = function () {
return {
number      : number,
origin      : origin,
destination : destination
};
};

==============================
user in app.js


var flight = require('./flight');

flight.setOrigin('Kensington');
flight.setDestination('London');
flight.setNumber(356);

console.log(flight.getInfo());

Enable cross origin ( CORS ) in NodeJs

app.all('*', function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
res.header("Access-Control-Allow-Headers", "X-Requested-With,X-Powered-By,Content-Type");
if (req.method === 'OPTIONS') {
res.status(200).end();
} else {
next();
}
});

How to Replace Object With Named Parameters in ES6 ( Error handing of undefined in JavaScript )

function loadProfiles(userNames = [],  {profilesClass, reverseSort} = {}) {
   profilesClass = profilesClass || ".user-profile";
   reverseSort   = reverseSort   || false;

  if (reverseSort) {
    userNames = _reverse(userNames);
  }

  _loadProfilesToSideBar(userNames, profilesClass);
}

Add Param middleware to fix dynamic routing in express

app.param('name', function (request, response, next) {

  request.cityName = parseCityName(request.params.name);

  next();

});

Create custom logger module in nodejs

create file name: logger.js

module.exports = function (request, response, next) {
  var startTime = +new Date();
  var stream = process.stdout;
  var duration = null;

  response.on('finish', function () {
    duration = +new Date() - startTime;
    stream.write("This request took " + duration + " ms" + request + " "+ response);
  });
  
  next();
};

// used to log  the request and response and duration