我需要退货特定类别的产品以及该类别子类别中的以下产品,并且我将laravel多态用于此工作,您能帮我如何退货吗?
我使用递归函数来完成这项工作和这项工作,然后返回给我,但不是最优的
Product.php
/**
* Get all of the owning product_able models.
*/
public function product_able()
{
return $this->morphTo();
}
Category.php
/**
* Get the category's product.
*/
public function product()
{
return $this->morphOne(Product::class, 'product_able');
}
/**
* Get all of the categories for the children.
*/
public function children()
{
return $this->hasMany(self::class, 'parent_id');
}
/**
* Get the category that owns the parent.
*/
public function parent()
{
return $this->belongsTo(self::class, 'parent_id');
}
CategoryController.php
public function detail()
{
$products = new Collection();
$category = Category::ofUuid(request('category_uuid'))->ofType(config('constants.category.type.main'))->active()->first();
$products->push($category->product);
$subCategory = CategoryDetailResource::collection($category->children);
$products->push(self::check($category));
$productsFlatten = $products->flatten();
$productsFilterNull = Utility::filterNullValue($productsFlatten);
$productsPagination = Utility::paginate_collection($productsFilterNull, 15);
$productsResource = ProductResource::collection($productsPagination);
return response()->json([
'sub_categories' => $subCategory,
'products' => $productsResource
]);
}
public function check($categories)
{
$data = [];
foreach ($categories->children as $category) {
if ($category->product) {
if ($category->product->status == config('constants.product.status.active'))
$data[] = [
'product' => $category->product,
'children' => self::check($category),
];
$data[] = [
'children' => self::check($category),
];
}
}
return $data;
}
CategoryResource.php
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
{
return [
'uuid' => $this->uuid ?? '',
'title' => $this->describe->title ?? '',
'path' => $this->file->path ?? config('constants.default.category.image'),
'children' => CategoryDetailResource::collection($this->children()->ofType(0)->get() ?? ''),
];
}
我的输出:
{
"sub_categories": [
{
"uuid": "afcac5f8-95d4-42f5-af7e-b7db943c5d09",
"title": "other",
"path": "https://dkstatics-public.digikala.com/digikala-products/2326879.jpg?x-oss-process=image/resize,m_lfit,h_500,w_500/quality,q_80",
"children": [
{
"uuid": "c7b5d0f8-25af-437e-ac7e-4103c8bc1960",
"title": "cover",
"path": "https://dkstatics-public.digikala.com/digikala-products/5157933.jpg?x-oss-process=image/resize,m_lfit,h_500,w_500/quality,q_80",
"children": []
},
{
"uuid": "9cd0c171-e46d-4e1d-b3b4-5c71a79fdd39",
"title": "power",
"path": "https://dkstatics-public.digikala.com/digikala-products/834627.jpg?x-oss-process=image/resize,m_lfit,h_500,w_500/quality,q_80",
"children": []
},
{
"uuid": "f6b728a7-674f-44b8-9a8c-70d891283af7",
"title": "headphone",
"path": "https://dkstatics-public.digikala.com/digikala-products/2737272.jpg?x-oss-process=image/resize,m_lfit,h_500,w_500/quality,q_80",
"children": []
},
{
"uuid": "a8160991-9750-4473-9252-dc4b53eef62f",
"title": "holder",
"path": "https://dkstatics-public.digikala.com/digikala-products/2027918.jpg?x-oss-process=image/resize,m_lfit,h_500,w_500/quality,q_80",
"children": [
{
"uuid": "3216cd11-ba0e-431f-96b1-6665b9b6b420",
"title": "car",
"path": "https://dkstatics-public.digikala.com/digikala-products/4635214.jpg?x-oss-process=image/resize,m_lfit,h_500,w_500/quality,q_80",
"children": []
}
]
}
]
},
{
"uuid": "a017d012-313c-47d9-8146-965ba0fb4bb3",
"title": "mobile",
"path": "https://dkstatics-public.digikala.com/digikala-products/4560689.jpg?x-oss-process=image/resize,m_lfit,h_500,w_500/quality,q_80",
"children": []
},
{
"uuid": "eff6c9ff-7f1a-401b-ba90-df2b3e3b3567",
"title": "camera",
"path": "https://dkstatics-public.digikala.com/digikala-products/266661.jpg?x-oss-process=image/resize,m_lfit,h_500,w_500/quality,q_80",
"children": []
},
{
"uuid": "a935ebb0-5392-4d56-b30d-942080ae1957",
"title": "laptop",
"path": "https://dkstatics-public.digikala.com/digikala-products/5177558.jpg?x-oss-process=image/resize,m_lfit,h_500,w_500/quality,q_80",
"children": []
}
],
"products": [
null
]
}