php Laravel 5.5资源原生变换器

Posted

tags:

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

protected $load_relationships;

    public function __construct()
    {
        
        $this->load_relationships = ['vendor','approval_status','appservice_components.component','categories'];
    }

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->load($this->load_relationships));

        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 parent::toArray($request);
    }
}

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

Laravel 5.5 将会要求 PHP 7.0+

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

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

php 在Laravel 5.5中更改公共路径

Laravel 5.5资源控制器行为不端

laravel 5.5 《电商实战 》辅助函数