我已经阅读了 Laravel 5 文档,我看到它支持数据库中的 json 类型字段,但我注意到了非常奇怪的行为。
这是我的 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');
});
然后我为数据库播种:
Subject::create(['name' => 'Programming 1',
'ects' => '0',
'studies' => '{"program":"Computer Science","year":"1"}']);
服务器响应看起来像这样:
{
"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
}
}
注意 response field studies 中的\",laravel 为我生成的 "pivot"字段结构正确,也是 json 类型的字段。
当我查看 phpMyAdmin 时,studies 字段的值看起来很正常。 (没有\")
我的服务器响应代码:
$subjects = Profesor::find($id)->subjets()->get();
return response()->json($subjects);
我是否正确地为数据库设置了种子,或者问题出在我在服务器上返回值时?
我知道我可以在客户端删除符号“\”来解决这个问题,但这是我最后的选择,因为它不够干净。
编辑:
我通过在我的模型类中添加一个数组转换来解决它:
protected $casts = [
'name' => 'string',
'ects' => 'integer',
'studies' => 'array'
];
文档可以看here
最佳答案
看起来您正在对整个 eloquent 集合 进行 json 编码,当您使用 get() 方法 (http://laravel.com/docs/5.1/eloquent-collections) 时会返回该集合。
来自 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_encode() 将整个集合转换为 json,这将假定您的 json 字段是一个字符串,因此已将其转义。
关于php - json类型字段的Laravel数据库在返回值时添加“,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34106804/
我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i
我有一个表单,其中有很多字段取自数组(而不是模型或对象)。我如何验证这些字段的存在?solve_problem_pathdo|f|%>... 最佳答案 创建一个简单的类来包装请求参数并使用ActiveModel::Validations。#definedsomewhere,atthesimplest:require'ostruct'classSolvetrue#youcouldevencheckthesolutionwithavalidatorvalidatedoerrors.add(:base,"WRONG!!!")unlesss
我想向我的Controller传递一个参数,它是一个简单的复选框,但我不知道如何在模型的form_for中引入它,这是我的观点:{:id=>'go_finance'}do|f|%>Transferirde:para:Entrada:"input",:placeholder=>"Quantofoiganho?"%>Saída:"output",:placeholder=>"Quantofoigasto?"%>Nota:我想做一个额外的复选框,但我该怎么做,模型中没有一个对象,而是一个要检查的对象,以便在Controller中创建一个ifelse,如果没有检查,请帮助我,非常感谢,谢谢
在我的Controller中,我通过以下方式在我的index方法中支持HTML和JSON:respond_todo|format|format.htmlformat.json{renderjson:@user}end在浏览器中拉起它时,它会自然地以HTML呈现。但是,当我对/user资源进行内容类型为application/json的curl调用时(因为它是索引方法),我仍然将HTML作为响应。如何获取JSON作为响应?我还需要说明什么? 最佳答案 您应该将.json附加到请求的url,提供的格式在routes.rb的路径中定义。这
我可以得到Infinity和NaNn=9.0/0#=>Infinityn.class#=>Floatm=0/0.0#=>NaNm.class#=>Float但是当我想直接访问Infinity或NaN时:Infinity#=>uninitializedconstantInfinity(NameError)NaN#=>uninitializedconstantNaN(NameError)什么是Infinity和NaN?它们是对象、关键字还是其他东西? 最佳答案 您看到打印为Infinity和NaN的只是Float类的两个特殊实例的字符串
我不确定传递给方法的对象的类型是否正确。我可能会将一个字符串传递给一个只能处理整数的函数。某种运行时保证怎么样?我看不到比以下更好的选择:defsomeFixNumMangler(input)raise"wrongtype:integerrequired"unlessinput.class==FixNumother_stuffend有更好的选择吗? 最佳答案 使用Kernel#Integer在使用之前转换输入的方法。当无法以任何合理的方式将输入转换为整数时,它将引发ArgumentError。defmy_method(number)
我知道我可以指定某些字段来使用pluck查询数据库。ids=Item.where('due_at但是我想知道,是否有一种方法可以指定我想避免从数据库查询的某些字段。某种反拔?posts=Post.where(published:true).do_not_lookup(:enormous_field) 最佳答案 Model#attribute_names应该返回列/属性数组。您可以排除其中一些并传递给pluck或select方法。像这样:posts=Post.where(published:true).select(Post.attr
有时我需要处理键/值数据。我不喜欢使用数组,因为它们在大小上没有限制(很容易不小心添加超过2个项目,而且您最终需要稍后验证大小)。此外,0和1的索引变成了魔数(MagicNumber),并且在传达含义方面做得很差(“当我说0时,我的意思是head...”)。散列也不合适,因为可能会不小心添加额外的条目。我写了下面的类来解决这个问题:classPairattr_accessor:head,:taildefinitialize(h,t)@head,@tail=h,tendend它工作得很好并且解决了问题,但我很想知道:Ruby标准库是否已经带有这样一个类? 最佳
我正在尝试解析一个CSV文件并使用SQL命令自动为其创建一个表。CSV中的第一行给出了列标题。但我需要推断每个列的类型。Ruby中是否有任何函数可以找到每个字段中内容的类型。例如,CSV行:"12012","Test","1233.22","12:21:22","10/10/2009"应该产生像这样的类型['integer','string','float','time','date']谢谢! 最佳答案 require'time'defto_something(str)if(num=Integer(str)rescueFloat(s
我正在玩HTML5视频并且在ERB中有以下片段:mp4视频从在我的开发环境中运行的服务器很好地流式传输到chrome。然而firefox显示带有海报图像的视频播放器,但带有一个大X。问题似乎是mongrel不确定ogv扩展的mime类型,并且只返回text/plain,如curl所示:$curl-Ihttp://0.0.0.0:3000/pr6.ogvHTTP/1.1200OKConnection:closeDate:Mon,19Apr201012:33:50GMTLast-Modified:Sun,18Apr201012:46:07GMTContent-Type:text/plain