Laravel 回傳 json的亂碼解決方式

在Laravel中我們要產出json格式的話,一般只要
[php]
Route::get(‘json’, function () {
$users = DB::table(‘users’)->get();
return Response::json($users);
}
[/php]
但是遇到中文時,會出現有亂碼的問題,解決很簡單
[php]
Route::get(‘json’, function () {
$headers = array(‘Content-Type’ => ‘application/json; charset=utf-8’);
$users = DB::table(‘users’)->get();
return Response::json($users, 200, $headers, JSON_UNESCAPED_UNICODE);
});
[/php]

Laravel 5.3 連接 SQL Server

一、下載 https://github.com/superlevin/xamppforlaravel

二、config/database.ph的connections增加
[php]
‘sqlsrv’ => [
‘driver’ => ‘sqlsrv’,
‘host’ => env(‘DB_HOST’, ‘localhost’),
‘database’ => env(‘DB_DATABASE’, ‘dbname’),
‘username’ => env(‘DB_USERNAME’, ‘dbuser’),
‘password’ => env(‘DB_PASSWORD’, ‘dbpassword’),
‘charset’ => ‘utf8’,
‘prefix’ => ”,
],
[/php]
三、.env修改連線
[php]
DB_CONNECTION=sqlsrv
DB_HOST=localhost
DB_PORT=1433
DB_DATABASE=dbname
DB_USERNAME=dbuser
DB_PASSWORD=dbpassword
[/php]
四、測試是否可以產生
routes/web.php
[php]
Route::get(‘/’, function () {
var_dump( DB::table(‘tablename’)->first() );
});
[/php]