簡體   English   中英

Laravel 5.2更改數據庫列名引用

[英]Laravel 5.2 change database column name reference

您好我正在嘗試更改如何訪問數據庫列名而不更改名稱,例如,我的列名稱是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"
        }
      ]
    }

之前我沒有聽說過$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()結果中。

     protected $hidden = ['idCountry', 'idCompany', 'initials', 'thumbnail', 'icon', 'status', 'createTime', 'updateTime', 'resourceType']; 
  2. 您需要創建一個getNameAttribute() 訪問器方法 ,只要您嘗試訪問name屬性,就會調用該方法

     public function getNameAttribute() { return $this->resourceType; } 
  3. 您需要為$appends數組添加name ,以便它將包含在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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM