twopiers
1 month ago
240
12
Eloquent

Eloquent save converting unicode characters

Posted 1 month ago by twopiers

I have an app that pulls some information from an API and simply saves it (via an Eloquent model) to MySQL. I'm running into an issue when a string from the API contains an en dash character. Somewhere along the way (I don't know if this is an Eloquent or PHP or MySQL thing), the en dash character gets converted to something else.

Some code:

//lookup value via API and store in $response
//for this example, $response['location'] = 'Bordeaux–Mérignac Airport'
//the hyphen is an en dash character
$location = $response['location'];

dd($location); // 'Bordeaux–Mérignac Airport'

So far, the en dash is just fine. If I copy/paste this value into a unicode converter tool (like this one), it says the en dash character's hex value is

– 

which is correct (I think). Then I save:

$some_record = App\SomeModel::find(22); //grab my model
$some_record->location = $location;
$some_record->save();

dd($some_record->location); // 'Bordeaux–Mérignac Airport'

Now the en dash is gone, replaced with a large space character. If I copy/paste the value into a unicode converter tool and convert it, it says the character is now

– 

which is something called a START OF GUARDED AREA character.

The MySQL table's charset is set to 'utf8mb4' and the collation is set to 'utf8mb4_unicode_ci'.

I'm clearly doing something wrong or forgetting a step somewhere. I'm at the end of my knowledge gap on this. Any advice would be most welcome.

Thanks for listening.

Please sign in or create an account to participate in this conversation.