Итак, я пытался получить отношение второго уровня из моего "CategoryResource", но оно не работает, вот мой код:
Во-первых, моя модель:
public function children()
{
return $this->hasMany(Category::class, 'parent_id', 'id');
}
public function sub_children()
{
return $this->hasMany(Category::class, 'parent_id');
}
и затем "CategoryResource":
$data = [
'id' => $this->id,
'parent_id' => $this->parent_id,
'order' => $this->order,
'name' => $this->name,
'slug' => $this->slug,
'childs' => CategoryResource::collection($this->whenLoaded('children') && $this->whenLoaded('children')),
'created_at' => (string) $this->created_at,
'updated_at' => (string) $this->updated_at,
];
Мой контроллер
return CategoryResource::collection(Category::where('parent_id',null)->with('children.sub_children')->get());
В любом случае я могу получить свое вложенное отношение через ресурс laravel?