Categories
Javascript Laravel

Change DB Connection at Runtime in Laravel

\Config::set("database.connections.mysql", [
    "host" => "your-host.com",
    "driver" => "mysql",
    "database" => 'database-name',
    "username" => "username",
    "password" => "password",
]);
\DB::purge('mysql');
Categories
Laravel

PhpSpreadsheet Issue With Vertical Tabs (0x0B)

This article is about fixing the following error:

Failed to load path_to_project/storage/framework/laravel-excel/laravel-excel-eTdm3TlUf1neh8QbolfXmodtigO50x3x.html as a DOM Document

Recently I caught a bug where PHP excel parser (PhpSpreadsheet) was failing when parsing some rows fetched from the database. With a series of trial and error tests, I found the row that was causing the failure.

So, I copied the sentence in Sublime Text and saw that there was a hex character (0x0b) in the sentence. This character is known as a “Vertical Space” and is visually invisible in most editors. I couldn’t see this character in Mysql Workbench so I copied the whole row in Sublime text and saw the hex representation of this character. Below are some other representations of this character:

  • 0x0b
  • “\v”

To fix the issue, I ran the following SQL query to find all sentences containing this character and then removed them from the database:

SELECT text FROM messages WHERE text LIKE CONCAT('%',0x0b,'%');

It’s a good practice to filter your inputs and make sure you’re not storing unwanted characters like these in the database, they might bite you one day!

Categories
Laravel

How to Get All Redis Cache Keys in Laravel

TL;DR:

Cache::getRedis()->keys("*")

If you’ve set up your Laravel application with Redis and you want to see all the keys registered in your Redis, simply open Laravel Tinker using the following command:

php artisan tinker

And then run the following code snippet to get all the keys available in your redis:

Cache::getRedis()->keys("*")