I’m currently developing a Laravel 8 application that connects to SQL Server 2017, and I’m encountering issues when trying to store emoji symbols in an NVARCHAR column. While it succeeds when I utilize the Query Builder, it fails to do so with Eloquent.
Successful Example with DB Facade:
DB::table('cabang')->insert(['kodecabang' => '✅']);
Unsuccessful Attempt with Eloquent:
$cabang = new \App.Models rCabang();
$cabang->kodecabang = '✅';
$cabang->save(); // Leads to corrupted characters or errors
Using Eloquent causes the emoji to turn into question marks or produces UTF-8 encoding error messages.
Checks I’ve Conducted:
- Confirmed column type is NVARCHAR
- Laravel DB configuration is set to use utf8
- Attempted to filter and re-encode strings prior to saving
- File encoding is validated as UTF-8
- Direct SQL commands with N’ prefix work correctly in SQL Server Management Studio
My Questions:
- What is the reason Query Builder accurately handles Unicode while Eloquent does not?
- Can I configure Eloquent to automatically incorporate the N prefix for NVARCHAR types?
- Is the issue connected to the pdo_sqlsrv driver?
- Are there any solutions to ensure model->save() works with emoji without overhauling the entire app?
Converting everything to use DB facade isn’t feasible for my project, which heavily relies on Eloquent. Any assistance in resolving this matter would be greatly appreciated!