1

I'm trying to insert an arabic string to my table, but it's shown like تست.

I have tried to insert to the database table using eloquent and query builder, but the same issue is occurred.

DB::connection('ticketingsystem_new')->insert("Insert INTO [DatabaseName].[dbo]. 
[Table] (Reason,Feeling) VALUES (N'{$request->reason}','{$request->feeling}')");

The column data type is NVARCHAR(n).

Note : When I tried to insert an Arabic to this table directly on SQL management studio it works fine, but when using laravel framework it shows as تسØ

  • Is your column datatype `NVARCHAR(n)`? – Ilyes Jun 09 '19 at 11:01
  • Yes, it is NVARCHAR(n) – Eslam Kalash Jun 09 '19 at 11:06
  • Tbh, I don't know about laravel, but since you are using parameters I think all what you need is to declare them as a unicode string. – Ilyes Jun 09 '19 at 11:13
  • Check out in `config/database.php` _'charset' => 'utf8', 'collation' => 'utf8_unicode_ci',_ See [this post as it may helps](https://stackoverflow.com/questions/32867172/laravel-5-1-utf-8-saving-to-database). Also you don't need the `N` prefix since you're passing a parameter. – Ilyes Jun 09 '19 at 11:19
  • I have set 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci' but the same issue exist – Eslam Kalash Jun 09 '19 at 11:41
  • your reason is in arabic or feeling too? – Ketan Kotak Jun 09 '19 at 11:42
  • 1
    When I tried to insert an Arabic to this table directly on SQL management studio it works fine , but when using laravel framework it shows as تست – Eslam Kalash Jun 09 '19 at 11:51

2 Answers2

0

You need to ensure your PHP connection is properly configured.

'sqlsrv' => [
        'driver' => 'sqlsrv',
        'host' => env('DB_HOST', 'servername'),
        'database' => env('DB_DATABASE', 'dbname'),
        'username' => env('DB_USERNAME', 'username'),
        'password' => env('DB_PASSWORD', 'password'),
      ------->  'charset' => 'utf8',
        'prefix' => '',
        ],

and

DB::connection('ticketingsystem_new')->insert("Insert INTO [DatabaseName].[dbo]. 
[Table] (Reason,Feeling) VALUES (N'{$request->reason}','{N'$request->feeling}')");

or you can try with

$query = "[DatabaseName].[dbo].[Table] (Reason,Feeling) VALUES ";
   $query .= '(';
   $query .= "N'" . $request->reason . "', ";
   $query .= "N'" . $request->feeling . "'";
   $query .= ')';
Ketan Kotak
  • 942
  • 10
  • 18
0

Solved by Upgrade laravel version, I was using 5.1