Logging an Array in Laravel

  • September 04, 2015

Welcome to the incredibly popular Easy Laravel 5 companion blog. To celebrate the new edition's release (updated for Laravel 5.5!) use the discount code easteregg to receive 20% off the book or book/video package! » Buy the book

I regularly use Laravel's logger when debugging an application, keeping a terminal window open to an active view of the laravel.log file alongside the browser window.

You can actively view the latest additions to your log file using the tail command accompanied by the -f option:

$ tail -f storage/logs/laravel.log

This works on OS X and Linux, although I'm not sure how this is done on Windows, if you know please e-mail me so I can update this post. Once you're actively viewing the log file, you can log a message using Laravel's Log class:

\Log::info("This is a message from a controller");

Logging an array isn't so straightforward though, because PHP will not automatically convert an array to a string. Therefore you'll need to use Log in conjunction with the print_r function, like so:

Log::info(print_r($array, true));

Easy Laravel 5 readers learn about multiple debugging solutions throughout many chapters of the book.

Buy the book