Laravel集合强调错误 [英] Laravel collection accentuation error

查看:52
本文介绍了Laravel集合强调错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Laravel中编写的程序(WebAPI)遇到了很大的问题.

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

首先,这是软件的配置:

First, this is software's configuration:

  • Laravel Framework 5.4.30

  • Laravel Framework 5.4.30

SQL Server 2012

SQL Server 2012

我试图返回表"students"的Json,但是当字段"name"包含重音(例如José")时,此Json返回错误.如果不包含重音符号,则Json会返回所有行,例如'Maria'

I was trying to return a Json of the table "students", but this Json returned error when the field "name" contain accentuations, example 'José'. When it doesn't contain accentuations, the Json returned with all rows, example 'Maria'

当我在name字段的值之前意识到一个字母时,我看到了该集合的返回.

I saw the collection returned when I realized a letter before the value of the name field.

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);
        
    }

}

这个结果没有重点:

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

转换为Json:return json_encode($return);,返回:

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

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

但是..当结果突出时:

But.. when the result have a accentuations:

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

修复值前的字母"b" .

转换为Json(return json_encode($return);),返回: 杰森(Json)问题

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

这是文件"/config/database.php"上的连接配置

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

'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' => '',
        ]

救救我!

推荐答案

有一种简单的方法可以实现此目的,而不是返回json_encode尝试返回集合.

There is a simple way achieve this rather than returning a json_encode try to return a 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.

这篇关于Laravel集合强调错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆