php - Laravel 5.2 更改数据库列名引用

标签 php laravel eloquent

你好,我正在尝试更改如何访问数据库列名而不更改名称,例如,我的列名是 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/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() accessor method ,每当您尝试访问 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;
}

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

相关文章:

php - 在 Laravel、Vuejs 和 Inertia 中配置 TypeScript 时出现问题

php - 如何在全局范围内强制执行 PHP 文件?

php - Laravel 4.2 - 使用::firstOrCreate 并确定实际发生了什么?

mysql - Laravel Eloquent 在子查询中有两个 “WHERE NOT IN”

php - 数据库更改的实时更新通知 MYSQL PHP

php - Laravel 下拉列表

php - Laravel 5.3分页-Collection.php第432行中的ErrorException

拉维尔 4 : Installation error on Windows 8 XAMPP/WAMP

mysql - 如何在 db raw laravel 中添加条件?

laravel - Laravel 中返​​回 ResourceCollection 时如何返回 HTTP 状态代码