你好,我正在尝试更改访问数据库列名的方法,而不更改名称。例如,我的列名是
这是我的模型:
resourceType
,但我想将其称为name
。同时,我希望响应JSON显示name
而不是resourceType
。在互联网上搜索时,我发现应该使用protected $maps = ['oldName' => 'newName'];
,但这并没有起作用。我想更改resourceType
,因为我认为表格名称和列名不应该相等:resourceType->resourceType
。这是我的模型:
<?php
namespace Knotion;
use Illuminate\Database\Eloquent\Model;
use Mappable, Mutable;
class CTL_ResourceType extends Model {
public $timestamps = false;
protected $table = "CTL_ResourceType";
protected $primaryKey = "idResourceType";
public $incrementing = false;
public static $snakeAttributes = false;
protected $hidden = ['idCountry', 'idCompany', 'initials', 'thumbnail', 'icon', 'status', 'createTime', 'updateTime'];
protected $fillable = ['name'];
protected $maps = ['resourceType' => 'name'];
protected $appends = ['name'];
public function resource() {
return $this->hasMany('Knotion\CTL_Resource', 'idResource' );
}
public function country() {
return $this->belongsTo('Knotion\CTL_Country', 'idCountry', 'idCountry');
}
public function company() {
return $this->belongsTo('Knotion\CTL_Company', 'idCompany', 'idCompany');
}
}
这是我收到的响应JSON。你可以看到,resourceType
仍然存在,而不是name
{
"total": 16,
"per_page": 15,
"current_page": 1,
"last_page": 2,
"next_page_url": "http://localhost:8000/krb/api/resources?page=2",
"prev_page_url": null,
"from": 1,
"to": 15,
"data": [
{
"idResource": "4e8f1ece-f666-11e5-8137-0f7932903a75",
"productionKey": "238493ujjsl",
"title": "ElTitle16",
"description": "ElDescription16",
"minimumAge": "4",
"maximumAge": "15",
"fileName": "ElFileName16",
"extension": ".png",
"URL": "ElURL16",
"createTime": "2016-03-30 04:58:16",
"creatorUser": {
"idUser": "85cf125c-f5ff-11e5-8137-0f7932903a75",
"name": "Roberto"
},
"creationCountry": {
"idCountry": "f03a75a0-f5ff-11e5-8137-0f7932903a75",
"country": "Estados Unidos"
},
"resourceType": {
"idResourceType": "5c902028-f601-11e5-8137-0f7932903a75",
"resourceType": "TípodeRecurso3"
},
"tags": [
{
"idTag": "40c6a114-f520-11e5-8137-0f7932903a75",
"name": "ElTag1"
}
],
"quickTags": [
{
"idQuickTag": "679bc8f0-f520-11e5-8137-0f7932903a75",
"name": "ElQuickTag4"
}
],
"relatedTo": [
{
"idRelatedTo": "7beddc6c-f520-11e5-8137-0f7932903a75",
"name": "ElRelatedTo3"
}
]
}