# 嵌套关系

API相对复杂的一方面是处理嵌套关系，使用API​​资源的最简单方法是向返回的数组添加一个键，该键设置为API资源集合，如例13-23所示。

{% code title="Example 13-23. A simple included API relationship" %}

```php
public function toArray() {
    return [
        'name' => $this->name,
        'breed' => $this->breed,
        'friends' => DogResource::collection($this->friends),
    ]; 
}
```

{% endcode %}

你可能也想添加判断属性，你可以在请求时才嵌套或者只有在已经传入的Eloquent对象上已经加载了时候嵌套，如示例13-24。

{% code title="Example 13-24. Conditionally loading API relationship" %}

```php
<?php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\ResourceCollection;

class DogCollection extends ResourceCollection
{
    /**
     * Transform the resource collection into an array.
     *
     * @param  \Illuminate\Http\Request $request
     * @return array
     */
    public function toArray($request)
    {
        return [
            'name' => $this->name,
            'breed' => $this->breed,
            // Only load this relationship if it's been eager-loaded
            'bones' => BoneResource::collection($this->whenLoaded('bones')),
            // Or only load this relationship if the URL asks for it
            'bones' => $this->when(
                $request->get('include') == 'bones',
                BoneResource::collection($this->bones)
            ),
        ];
    }
}
```

{% endcode %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://liseen315.gitbook.io/laravel/bian-xie-apis/api-zi-yuan/qian-tao-guan-xi.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
