php Laravel 5.5资源定制变压器

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php Laravel 5.5资源定制变压器相关的知识,希望对你有一定的参考价值。

<?php

namespace App\Http\Resources\V1\ApplicationService;

use Illuminate\Http\Resources\Json\ResourceCollection;

class ApplicationServiceResourceCollection extends ResourceCollection
{
    /**
     * Transform the resource collection into an array.
     *
     * @param  \Illuminate\Http\Request
     * @return array
     */
    public function toArray($request)
    {
        return [
            'data' => $this->collection->transform(function($page){
                return [
                    'uuid' => $page->uuid,
                    'vendor_id' => $page->vendor_id,
                    'appservice_name' => $page->appservice_name,
                    'description' => $page->description,
                    'version' => $page->version,
                    'approval_status_id' => $page->approval_status_id,
                    'is_publish' => (boolean) $page->is_publish,
                    'vendor'=> $page->vendor,
                    'approval_status'=> $page->approval_status,
                    'appservice_components'=> $page->appservice_components->load('component'),
//                    'component'=> optional($page->appservice_components)->component,
                ];
            }),
        ];
    }
}
public function store(StoreVendorApplicationServiceRequest $request, $vendor_id)
    {
        $vendor = $this->vendor->findOrFailUuid($vendor_id);
 
        $request_data = $request->all();
 
        //create application service
 
        $application_service = $vendor->appservices()->create($request_data);
 
        //create application service components
 
        $application_service->appservice_components()->create($request_data);
 
        //create applicaton service categories
 
        if ($request->filled('categories')) {
            $application_service->categories()->attach($request->categories);
        }
 
        //create applications service tags
 
        if ($request->filled('tags')) {
            $application_service->attachTags($request->tags);
        }
 
        $resource = new ApplicationServiceResource($application_service);
 
        return $this->responseCreated($resource);
    }
<?php

namespace App\Http\Resources\V1\ApplicationService;

use Illuminate\Http\Resources\Json\Resource;

class ApplicationServiceResource extends Resource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request
     * @return array
     */
    public function toArray($request)
    {
        return [
            'uuid' => $this->uuid,
            'appservice_name' => $this->appservice_name,
            'description' => $this->description,
            'version' => $this->version,
            'approval_status_id' => $this->approval_status_id,
            'is_publish' => (boolean) $this->is_publish,
            'vendor_id' => $this->vendor_id,
            'vendor'=> $this->vendor,
            'approval_status'=> $this->approval_status,
            'appservice_components'=> $this->appservice_components->load('component'),
//            'component'=> optional($this->appservice_components)->component,
        ];
    }
}

以上是关于php Laravel 5.5资源定制变压器的主要内容,如果未能解决你的问题,请参考以下文章

Laravel 变压器与资源

Laravel 5.5 将会要求 PHP 7.0+

如何在 laravel 5.5 中将数组作为 API 资源返回

phpunit 测试资源 laravel 5.5 返回集合而不是 json

php 在Laravel 5.5中更改公共路径

Laravel 5.5资源控制器行为不端