I've red the Laravel 5 documentation, and I've seen that it supports json type fields in database, but I've noticed really weird behavior.
我已经删除了Laravel 5文档,我已经看到它支持数据库中的json类型字段,但我注意到了非常奇怪的行为。
This is my table:
这是我的表:
Schema::create('subjects', function ($table) {
$table->increments('id');
$table->string('name', 100);
$table->integer('ects');
$table->json('studies'); // this is my json type field
$table->date('created_at');
$table->date('updated_at');
});
Then I seed the database with:
然后我用以下方法为数据库播种
Subject::create(['name' => 'Programming 1',
'ects' => '0',
'studies' => '{"program":"Computer Science","year":"1"}']);
Server response looks something like this:
服务器响应看起来像这样:
{
"id": 15,
"name": "Programming 1",
"brEcts": 0,
"studies": "{\"program\":\"Computer Science\",\"year\":\"1\"}",
"created_at": "2015-12-05 00:00:00",
"updated_at": "2015-12-05 00:00:00",
"pivot": {
"profesor_id": 20,
"subject_id": 15
}
}
Notice the \" in response field studies, and the "pivot" field that laravel generated for me is correctly structured, and is also json type field.
请注意\“在响应字段研究中,laravel为我生成的”pivot“字段是正确构造的,也是json类型字段。
When I looked into phpMyAdmin, value of studies field looks normal. (without \")
当我查看phpMyAdmin时,研究领域的价值看起来很正常。 (没有\“)
My server code for response:
我的响应服务器代码:
$subjects = Profesor::find($id)->subjets()->get();
return response()->json($subjects);
Did I seed the database correctly, or the problem is when I return value on server?
我是否正确播种了数据库,或者问题是我在服务器上返回值?
I know that I can solve this problem on client side deleting symbols "\" but that is my last option since its not clean enough.
我知道我可以在客户端解决这个问题,删除符号“\”,但这是我的最后一个选项,因为它不够干净。
EDIT:
I solved it by adding an array casts in my Model class:
我通过在我的Model类中添加一个数组转换来解决它:
protected $casts = [
'name' => 'string',
'ects' => 'integer',
'studies' => 'array'
];
Documentation can be seen here
文档可以在这里看到
1 个解决方案
#1
2
It looks like you are json encoding the entire eloquent collection
, which is returned when you use the get()
method (http://laravel.com/docs/5.1/eloquent-collections).
看起来你是json编码整个eloquent集合,当你使用get()方法(http://laravel.com/docs/5.1/eloquent-collections)时返回它。
From the laravel docs:
来自laravel文档:
The json method will automatically set the Content-Type header to application/json, as well as convert the given array into JSON using the json_encode PHP function:
json方法将自动将Content-Type头设置为application / json,并使用json_encode PHP函数将给定的数组转换为JSON:
So essentially, you are converting the entire collection to json using json_encode()
which will assume that your json
field is a string and has therefore escaped it.
所以基本上,你是使用json_encode()将整个集合转换为json,这将假设你的json字段是一个字符串,因此将其转义。
#1
2
It looks like you are json encoding the entire eloquent collection
, which is returned when you use the get()
method (http://laravel.com/docs/5.1/eloquent-collections).
看起来你是json编码整个eloquent集合,当你使用get()方法(http://laravel.com/docs/5.1/eloquent-collections)时返回它。
From the laravel docs:
来自laravel文档:
The json method will automatically set the Content-Type header to application/json, as well as convert the given array into JSON using the json_encode PHP function:
json方法将自动将Content-Type头设置为application / json,并使用json_encode PHP函数将给定的数组转换为JSON:
So essentially, you are converting the entire collection to json using json_encode()
which will assume that your json
field is a string and has therefore escaped it.
所以基本上,你是使用json_encode()将整个集合转换为json,这将假设你的json字段是一个字符串,因此将其转义。