您好,我正在尝试更改如何在不更改名称的情况下访问数据库列名,例如,我的列名是resourceType,但我想将其命名,而且我希望响应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
{
  "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/eloquence包的一部分。
在这种情况下,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...
}

编辑
如果你想在没有软件包的情况下完成这项工作,你需要做三件事:
您需要将resourceType添加到$hidden数组中,这样它就不会出现在toArray()/toJson()结果中。
protected $hidden = ['idCountry', 'idCompany', 'initials', 'thumbnail', 'icon', 'status', 'createTime', 'updateTime', 'resourceType'];

您需要创建一个getNameAttribute()accessor method,当您试图访问name属性时,将调用该属性。
public function getNameAttribute() {
    return $this->resourceType;
}

您需要将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;
}

关于php - Laravel 5.2更改数据库列名称引用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36324086/

10-11 05:38