Black Friday: the Laravel testing course is $149 instead of $249, through Dec 7. See the course →
logo

Can we log data with complex placeholders?

Or only simple scalar types?

Joel Clermont
Joel Clermont
2025-10-30

From our daily Laravel tips email. Get the next one in your inbox.

In a previous tip, I explained why you should use placeholders in logs.

Someone recently asked me how these placeholders would work with data types like arrays.

For example, what if you wanted to do this? Would it work?

$params = ['key' => 'foo', 'keys' => ['aaa', 'bbb']];

Log::info("Error {key} not in {keys}", $params);

The quick answer is yes, it works! You'd get output like this:

Error foo not in array["aaa","bbb"] {"key":"foo","keys":["aaa","bbb"]} 

The $params value has to be an array, but each of the keys within that array can have values of other data types. As long as PHP can cast that data type to a string, it will display.

PHP's built-in array to string conversion has us covered here, but if we had a more complex data type, or wanted more control over the conversion, we could manually cast the value and not rely on the logger to do it automatically.

Here to help,

Joel

P.S. I bet you have a Laravel question rolling around in your head right now that would be perfect for our Mastering Laravel community. Come ask it!

Want a tip like this in your inbox every weekday?

Each one takes less than 2 minutes to read. Join 4,000+ Laravel developers who already get them.

email
No spam. Only real-world advice.
Toss a coin in the jar if you found this helpful.