Emoji not inserting into NVARCHAR column via Eloquent in Laravel, works with Query Builder

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:

  1. What is the reason Query Builder accurately handles Unicode while Eloquent does not?
  2. Can I configure Eloquent to automatically incorporate the N prefix for NVARCHAR types?
  3. Is the issue connected to the pdo_sqlsrv driver?
  4. 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!

This happens because Eloquent handles parameter binding differently than Query Builder. Eloquent uses prepared statements that don’t automatically add the N prefix for Unicode strings, while Query Builder handles this more directly. I ran into the same thing when migrating legacy data with special characters. You can fix it by adding a mutator to your Cabang model that forces Unicode handling:

public function setKodecabangAttribute($value)
{
    $this->attributes['kodecabang'] = mb_convert_encoding($value, 'UTF-8', 'UTF-8');
}

Also make sure your database connection config has 'charset' => 'utf8' and 'prefix_indexes' => true. This keeps Eloquent working normally while fixing Unicode storage without changing your whole app.

that’s interesting! have you checked if the db collation settings are set right for unicode? also, sometimes using json_encode() on the emoji before saving can help. any other specific characters you’re encountering issues with, or is it just this one?

had the same problem with sqlsrv driver. add 'options' => [PDO::SQLSRV_ENCODING_UTF8 => true] to your database config in config/database.php. Eloquent doesn’t always use the same PDO options as query builder by default. fixed it for me with chinese characters.