简体   繁体   English

Laravel API 资源集合从其他资源返回特定字段

[英]Laravel API Resource Collection return specific fields from other resource

I am working on an api that will be accessible via a mobile app.我正在开发一个可以通过移动应用程序访问的 api。

I have defined resources and collections for respective endpoints.我已经为各个端点定义了资源和集合。 My problem is now is that I want to return different api json data based on what ever collection.我现在的问题是我想根据收集的内容返回不同的 api json 数据。

Here is an example这是一个例子

Provinces has cities and suburbs, so in json format I need to have省份有城市和郊区,所以我需要以json格式

    "data": [
        {
            "id": 1,
            "name": "Eastern Cape",
            "cities": [
                {
                    "name": "Alice"
                },
            ],
            "suburbs": [
                    "name": "Suburb 1"
            ]
        },
]

I want different data when the cities resource is called in news api collection当在新闻 api 集合中调用城市资源时,我想要不同的数据

    "data": [
        {
            "id": 1,
            "name": "Eastern Cape",
            "cities": [
                {
                    "name": "Alice",
                    "municipality": "municipality name",
                },
            ],
            "suburbs": [
                    "name": "Suburb 1",
                    "ward_number": "ward 1"
            ]
        },
]

This is an NewsResource Api这是一个 NewsResource Api

    public function toArray($request)
    {
        // return parent::toArray($request);
        return [
            'id'=> $this->id,
            'title' => $this->title,
            'slug' => $this->slug,
            'content' => $this->content,
            'created_at' => $this->created_at,
            'category_id' => $this->news_category_id,
            'featured_image' => url($this->featured_image),
            'author' => new UserResource($this->user),
            'category' => new NewsCategoryResource($this->category), //Only category Name to be displayed
            'municipality' => new MunicipalityResource($this->municipality), // Only Municipality Name to be displayed
            'comments' => new NewsCommentResource($this->comments),
        ];
    }

I don't really know what is your code structure, but hope this help for you我真的不知道你的代码结构是什么,但希望这对你有帮助

You may use different queries, for example您可以使用不同的查询,例如

/** For the obvious resource calling */
return SomeResource::collection (SomeModel::with(['suburbs', 'cities'])
    /** Other filter queries */
    ->get());

/** For the cities resource calling */
return SomeResource::collection (SomeModel::with(['suburbs:id,ward_number', 'cities:id,municipality'])
    /** Other filter queries */
    ->get());

In your Resource class which you use for the cities/suburbs data, do it like so在用于城市/郊区数据的 Resource 类中,这样做

return [
    /** Some data which you need */
    'municipality' => $this->when($this->municipality !== null, $this->municipality),
    'ward_number' => $this->when($this->ward_number !== null, $this->ward_number),
];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM