Laravel collection accentuation error

I have a big problem with my program (WebAPI) made in Laravel.

First is the software configuration:

  • Laravel Framework 5.4.30

  • SQL Server 2012


I tried to return Json from the "students" table, but this Json returned an error when the "name" field contains accentuation like "José". When it contains no accentuation, Json is returned with all strings, like "Maria"

I saw the collection returned when I understood the letter before the name field value.

class StudentsController extends Controller
{
    public function index(Request $request){

        $class = $request['class'];

        $return = DB::table('students')
        ->where("class", $class)
        ->get();
        
        dd($return);
        
        return json_encode($return);
        
    }

}
      

Run codeHide result


This result has no emphasis:

Collection {#289
  #items: array:1 [
    0 => {#284
      +"id": "2"
      +"name": "Maria"
      +"class": "B"
    }
  ]
}
      

Run codeHide result


Convert to Json:, return json_encode($return);

return:

[{"id":"2","name":"Maria","class":"B"}]

      

But .. when the result has accentuation:

Collection {#289
  #items: array:1 [
    0 => {#284
      +"id": "1"
      +"name": b"José"
      +"class": "A"
    }
  ]
}
      

Run codeHide result


Restore the letter "b" to a value.

Convert to Json ( return json_encode($return);

), return: John problem

This is the connection configuration in the "/config/database.php" file

'sqlsrv' => [
            'driver' => 'sqlsrv',
            'host' => env('DB_HOST'),
            'port' => env('DB_PORT'),
            'database' => env('DB_DATABASE'),
            'username' => env('DB_USERNAME'),
            'password' => env('DB_PASSWORD'),
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
            'prefix' => '',
        ]

      

Help me!

0


source to share


1 answer


There is an easy way to achieve this, rather than returning json_encode

an attempt to return the collection.

class StudentsController extends Controller
{
   public function index(Request $request){

    $class = $request['class'];

    $return = DB::table('students')
    ->where("class", $class)
    ->get();


    return $return;

   }
}

      



It will return the result as expected.

0


source







All Articles