3

我使用 Laravel 6.x,下面是我的响应 JSON。

{
    "data": [
        {
            "id": 1,
            "name": "quam",
            "parent_id": 0
        },
        {
            "id": 2,
            "name": "quia",
            "parent_id": 1
        },
        {
            "id": 3,
            "name": "beatae",
            "parent_id": 1
        },
        {
            "id": 4,
            "name": "aut",
            "parent_id": 2
        },
        {
            "id": 5,
            "name": "provident",
            "parent_id": 0
        },
        {
            "id": 6,
            "name": "voluptate",
            "parent_id": 0
        },
        {
            "id": 7,
            "name": "vel",
            "parent_id": 2
        },
        {
            "id": 8,
            "name": "sed",
            "parent_id": 3
        },
        {
            "id": 9,
            "name": "voluptates",
            "parent_id": 0
        },
        {
            "id": 10,
            "name": "adipisci",
            "parent_id": 6
        },
        ...
    ]
}

但它希望是这样的:

{
    "data": [
        {
            "id": 1,
            "name": "quam",
            "children": [
                {
                   "id": 2,
                   "name": "quam"
                   "children":[
                       {
                           "id": 4,
                           "name": "aut"
                       },
                       {
                           "id": 7,
                           "name": "vel",
                           "children": [
                                ...
                           ]
                       }
                   ]
                 },
                 {
                   "id": 3,
                   "name": "quam",
                   "children":[
                       {
                           "id": 8,
                           "name": "sed"
                       }
                   ]
                 },
            ]
        },
        {
            "id": 5,
            "name": "provident"
        },
        {
            "id": 6,
            "name": "voluptate",
            "children": [
                 {
                      "id": 10,
                       "name": "adipisci"
                 }
            ]
        },
        {
            "id": 9,
            "name": "voluptates"
        },
        ...
}

实际上,我想删除parent_id属性并将children数组添加到由其他对象组成的每个对象都有这个parent_id。

分类资源.php

class CategoryResource extends JsonResource
{
    public function toArray($request)
    {
        return [
            'id' => $this->id,
            'name' => $this->name,
            'parent_id' => $this->parent_id,
        ];
    }
}

类别控制器.php

class CategoryController extends Controller
{
    public function index()
    {
        return CategoryResource::collection(Category::all());
    }
}

我怎样才能实现这个结构?

4

3 回答 3

7

从我看你的问题只是关系。要创建“树资源”,您必须加载很多关系。

恕我直言,这不是一个好的选择,特别是如果您必须加载大量元素,但一般来说,像这样的结构可能是一个危险的瓶颈。

无论如何...简单的方法是急切加载,因此您必须使用此属性添加基本模型(查看官方文档

class Parent extends Model {

  // [...]

  /**
   * The relationships that should always be loaded.
   *
   * @var array
   */
  protected $with = ['children'];

  // [...]

  public function children() {
     return $this->hasMany('whatever');
  }
}

接下来,您必须按如下方式更新您的 JSON 资源(为此,请查看有关资源关系的官方文档)。

class CategoryResource extends JsonResource
{

    public function toArray($request)
    {
        return [
            'id' => $this->id,
            'name' => $this->name,
            'parent_it' => $this->parent_id,
            'childrend' => ChildrenResource::collection($this->whenLoaded('children')),
        ];
    }
}

这样,由于每次请求 aParent它都会急切地加载其子节点,因此资源将递归地映射到Child每个关系到叶子的资源中。

于 2019-12-17T14:24:06.893 回答
3

假设类别是一个雄辩的模型,模型可以在关系中引用自己,并且这些关系可以是递归的。


class Category extends Model
{

    protected $hidden = ['parent_id'];

    public function children()
    {
        return $this->hasMany('App\Category', 'parent_id')->with('children');
    }
}

所以现在得到你想要的结构就像

Category::with('children:id,name,parent_id')->get('id', 'name', 'parent_id');

您必须在 select 语句中包含 parent_id 才能使关系起作用,但$hidden我添加到模型中的变量parent_id不会出现在序列化结果中。这里唯一需要注意的是,所有类别都有一个children属性,对于没有子类别的类别,该属性将为空。所以在你的 toArray 方法中,你必须检查空的 children[] 并排除它们

于 2019-12-17T14:23:55.353 回答
0

首先,您需要定义一个关系,以使用此方法检索没有父母的主要类别的孩子

/**
 * get sub product categories of this category
 *
 * @return mixed
 */
public function childCategories()
{
    return $this->hasMany(Category::class,'parent_id');
}

然后您需要使用此方法加载子类别的子项:

/**
 * get recursive all sub categories of this category.
 *
 * @return mixed
 */
public function childrenCategories()
{
    return $this->hasMany(Category::class,'parent_id')->with('childCategories');
}

现在您可以使用此静态函数检索它们

/**
 * get all Main categories with children.
 *
 * @return mixed
 */
public static function allMainCategoriesWithChildren()
{
    return self::whereNull('parent_id')->with('childrenCategories')->get();
}

现在您可以在资源中使用它或直接在控制器中返回它

use App\Category;
return Category::allMainCategoriesWithChildren();
于 2020-01-04T15:59:11.197 回答