dousheyan0375 2019-05-07 08:08
浏览 24

如何阅读子类别的所有产品

I need to return products of a particular category along with the following products in the subcategory of the category and I use laravel polymorphic for this work, can you help me how to return products?

I use from a recursive function for this work and this work and return to me but Not optimal

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() ?? ''),
    ];
}

my output:

{
"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
]

}

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥100 支付宝网页转账系统不识别账号
    • ¥15 基于单片机的靶位控制系统
    • ¥15 AT89C51控制8位八段数码管显示时钟。
    • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
    • ¥15 下图接收小电路,谁知道原理
    • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
    • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
    • ¥15 手机接入宽带网线,如何释放宽带全部速度
    • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
    • ¥15 ETLCloud 处理json多层级问题