Laravel 5.2更改数据库列名引用


Laravel 5.2 change database column name reference

你好,我正在尝试更改如何在不更改名称的情况下访问数据库列名,例如,我的列名是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"
        }
      ]
    }

我以前没有听说过$maps属性或Mappable,所以我做了一个快速搜索。看起来它们(以及Mutable)是jarektkaczyk/雄辩软件包的一部分。

在这种情况下,MappableMutable都是应该添加到类中的特征。此外,为了使它们正常工作,您还需要添加Eloquence特性。

文件顶部的use语句需要更改,以正确地寻址正确名称空间中的类名,然后需要将特征添加到类中:

<?php
namespace Knotion;
// import the class names
use Sofa'Eloquence'Mutable;
use Sofa'Eloquence'Mappable;
use Sofa'Eloquence'Eloquence;
use Illuminate'Database'Eloquent'Model;
class CTL_ResourceType extends Model  {
    // add the traits to the class
    use Eloquence, Mappable, Mutable;
    // code...
}

编辑

如果你想在没有包的情况下做到这一点,你需要做三件事:

  1. 您需要将resourceType添加到$hidden数组中,这样它就不会显示在toArray()/toJson()结果中。

    protected $hidden = ['idCountry', 'idCompany', 'initials', 'thumbnail', 'icon', 'status', 'createTime', 'updateTime', 'resourceType'];
    
  2. 您需要创建一个getNameAttribute()访问器方法,每当您尝试访问name属性时,都会调用该方法。

    public function getNameAttribute() {
        return $this->resourceType;
    }
    
  3. 您需要将name添加到$appends数组中,以便它将包含在toArray()/toJson()结果中。

    protected $appends = ['name'];
    

可选地,如果这感觉工作量太大,您可以始终覆盖toArray()方法(由toJson()调用)来强制执行命名约定,以及:

public function toArray() {
    // call parent method to get initial array results
    $array = parent::toArray();
    // set the new key with data
    $array['name'] = $array['resourceType'];
    // unset the old key
    unset($array['resourceType']);
    // return the array
    return $array;
}