Laravel 5.2更改数据库列名引用

时间:2016-03-31 04:19:32

标签: php laravel eloquent

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

1 个答案:

答案 0 :(得分:1)

之前我没有听说过$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...
}

修改

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

  1. 您需要将resourceType添加到$hidden数组中,以便它不会显示在toArray() / toJson()个结果中。< / p>

    protected $hidden = ['idCountry', 'idCompany', 'initials', 'thumbnail', 'icon', 'status', 'createTime', 'updateTime', 'resourceType'];
    
  2. 您需要创建一个getNameAttribute() accessor method,只要您尝试访问name属性,就会调用该帐户。

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

    protected $appends = ['name'];
    
  4. 可选地,如果感觉工作太多,您可以随时覆盖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;
    }