Laravel 5 + SQL Server and encoding errors

I am using Laravel 5 and SQL Server to get some data.

Whenever the data has some special characters (éê etc.), Laravel does not show the column value as if it were just nothing. (also not null).

I tried some SQL commands via tinker and got the same results. My titles don't show up when they have special characters.

The database is encoded with this collation: Latin1_General_CP1_CI_AS And my database.php looks like this:

<?php
      return [
      'connections' => [
                'mysql' => [/*my mysql connection*/],
                'dbmssql' => [
                            'driver'    => 'sqlsrv',
                            'host'      => 'host',
                            'database'  => '',
                            'username'  => 'username',
                            'password'  => 'password',
                            'prefix'    => '',
                            'collation' =>  'SQL_Latin1_General_CP1_CI_AS',
                            'charset'   =>  'latin1'
                        ],
                ]
      ];

      

We've tried almost everything. Changing collation / encoding, adding "Cast ()" to our SQL commands, etc. None of them work. We can't even get data with bad encodings. It just doesn't return anything.

Here is the tinker result:

  • The first result was added via Laravel, so the coding works with Laravel, but it's poorly coded on SQL Server
  • The second result has no special char. and display normally on both sides
  • The third result has a special char. on SQL Server Laravel doesn't render it at all.
      id: "1",
      titre: "ûéÉàÀêÊïÏçÇ",
      etat: "1",
      created_at: "May 29 2015 08:04:43:000PM",
      updated_at: "Jun  1 2015 03:24:44:000PM"
  },
  <stdClass #000000005a3cad49000000000aae376c> {
      id: "2",
      titre: "Administration",
      etat: "1",
      created_at: "Jun  1 2015 08:55:00:000AM",
      updated_at: "Jun  1 2015 08:55:00:000AM"
  },
  <stdClass #000000005a3cad4c000000000aae376c> {
      id: "3",
      titre: ,
      etat: "1",
      created_at: "Jun  1 2015 08:55:00:000AM",
      updated_at: "Jun  1 2015 08:55:00:000AM"
  },

      

Has anyone come across something similar?

Thank you for your time!

UPDATE

I finally did it using mutators!

Here is my code in case anyone comes across this situation.

<?php
#In my Model file.

public function getTitreAttribute($title){
    return utf8_encode($title);
}

public function setTitreAttribute($titre){
    $this->attributes['titre'] =  utf8_decode($titre);
}

      

+3


source to share





All Articles