I’m working on a Laravel 8 project that connects to Microsoft SQL Server 2017 (running on Windows Server 2012 R2) using PHP 7.4.9 and the sqlsrv/pdo_sqlsrv drivers. My goal is to insert Unicode characters (e.g., emoji like ✅) into a column defined as NVARCHAR.
When I insert using Laravel’s Query Builder:
DB::table('cabang')->insert(['kodecabang' => '✅']);
The Unicode characters are inserted correctly into the NVARCHAR column.
When I use Eloquent:
$cabang = new AppModelsCabang();
$cabang->kodecabang = '✅';
$cabang->save();
The character becomes corrupted (?, , etc), or I sometimes get:
Malformed UTF-8 characters, possibly incorrectly encoded
What I’ve Confirmed & Tried
- The target column is NVARCHAR
- Laravel DB config uses ‘charset’ => ‘utf8’
- Data is UTF-8 valid (verified via
mb_check_encoding
,iconv
, etc.) - Tried filtering and re-encoding strings before save (via Eloquent trait, middleware, model event saving())
- Using Laravel 8 Eloquent models and default connection
- Files are saved as UTF-8 without BOM
- I tried forcing conversions like:
iconv('UTF-8', 'UTF-8//IGNORE', $value)
Still no luck — the saved character is not the emoji, but broken.
- Inserting N’✅’ directly in SQL Server (SSMS) works fine
- Using raw DB::statement with
N'...'
also works - I noticed that neither my local nor server environment shows
ExtensionVer
when runningphp -i | find "sqlsrv"
.
However, Unicode insert via Eloquent works on local, but fails on the server.
This suggests the issue may not solely be the driver version, but perhaps the environment, OS-level encoding support, or how the driver is installed/configured on the server.
My Questions
- Does Laravel Eloquent or the PDO SQLSRV driver support Unicode binding correctly for NVARCHAR?
- Is it possible to make Eloquent insert Unicode data with the N’…’ prefix for NVARCHAR?
- Is the root cause likely my pdo_sqlsrv driver being outdated or not from Microsoft?
- Is switching to the official Microsoft SQLSRV drivers the only reliable solution to store Unicode via Eloquent ORM?
Notes
- I can’t rewrite the app to use DB::table() everywhere — it’s large and uses models extensively.
- I’m trying to find a way to store emoji via $model->save() reliably.
- Any ideas to force or simulate the Unicode-aware binding via Eloquent would be appreciated.
Thanks in advance for any insights or workaround!