【问题标题】:Laravel - transform field name with uppercase capital in json responseLaravel - 在json响应中用大写大写转换字段名称
【发布时间】:2019-12-14 17:31:21
【问题描述】:

我正在使用 Laravel 6.x,我有一个“Item”类,例如:

Schema::create('items', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->string('name');
        $table->string('type');
        $table->string('subtype');
    });

在控制器中,我有一条获取所有项目并返回 json 响应的路线,例如:

public function getItems()
{
    return response()->json([
        'datas' => Item::all()
    ]);
}

这个函数返回这个json:

{
  "datas": [
    {
      "id": 1,
      "name": "FIRE",
      "type": "9",
      "subtype": "5"
    },
    {
      "id": 2,
      "name": "FIRE",
      "type": "9",
      "subtype": "5"
    }
  ]
}

在返回json响应之前,我需要在每个字段的第一个字母上动态添加一个大写,而无需更改laravel字段名称和迁移。我需要在 Laravel 中将字段保持为小写。

我需要的是:

{
  "datas": [
    {
      "Id": 1,
      "Name": "FIRE",
      "Type": "9",
      "Subtype": "5"
    },
    {
      "Id": 1,
      "Name": "FIRE",
      "Type": "9",
      "Subtype": "5"
    }
  ]
}

我怎样才能简单地做到这一点?我有很多课程,很多领域都是真实的。谢谢!

【问题讨论】:

    标签: laravel


    【解决方案1】:

    您可以像这样使用Eloquent: API Resources

    php artisan make:resource Item
    

    然后您将在 Resources 文件夹中获得一个类。那是你编辑键的地方

    资源/Item.php

    namespace App\Http\Resources;
    
    use Illuminate\Http\Resources\Json\JsonResource;
    
    class Item extends JsonResource
    {
        /**
         * Transform the resource into an array.
         *
         * @param  \Illuminate\Http\Request  $request
         * @return array
         */
        public function toArray($request)
        {
            return [
                'Id' => $this->id,
                'Name' => $this->name,
                'Type' => $this->type,
                'Subtype' => $this->subtype,
            ];
        }
    }
    

    在您的控制器中,导入新创建的资源并使用它。

    控制器/ItemController.php

    use App\Http\Resources\Client as ClientResource;
    
    public function getItems()
    {
        return response()->json([
            'datas' => ItemResource::collection(Item::all());
        ]);
    }
    

    给你!

    【讨论】:

      【解决方案2】:

      您可以在模型中为name 属性(使用新名称)定义一个额外的getter。因此,您可以同时拥有名称和您定义的新属性。使用ucfirststrtolower

        public function getUCFirstNameAttribute($value)
       {
          return ucfirst(strtolower($value));
       }
      

      您可以定义项目资源。在toArray 方法中,您可以在name 字段上使用ucfirst

         return [
              ...
              'name' => ucfirst($this->name),
              ...
          ];
      

      这两种方法都可以让原始字段保持原样。

      【讨论】:

        【解决方案3】:

        简单通用的解决方案:

        public function toArray() {
            $array = parent::toArray();
            $newArray = array();
            foreach($array as $name => $value){
                $newArray[str_replace('_', '', ucwords($name, '_'))] = $value;
            }
            return $newArray;
        }
        

        把它放在一个模型类中,然后做:Item::all()->toArray()

        此函数将my_awesome_field 转换为MyAwesomeField

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-02-17
          • 1970-01-01
          • 2022-08-16
          • 1970-01-01
          • 2013-05-16
          • 2017-01-13
          相关资源
          最近更新 更多