第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

Laravel 從父類別及其所有子類別中獲取所有產(chǎn)品

Laravel 從父類別及其所有子類別中獲取所有產(chǎn)品

PHP
翻閱古今 2022-07-22 19:27:23
我正在嘗試從一個(gè)類別及其所有子類別中檢索產(chǎn)品。這是我的categories桌子:| id    | parent_id     | name          ||----   |-----------    |-------------  || 1     | NULL          | Electronics   || 2     | 1             | Computers     || 3     | 2             | Accessories   || 4     | 3             | Keyboards     |這是我的產(chǎn)品表:| id    | category_id   | name          ||----   |-------------  |-----------    || 1     | 2             | Product 1     || 2     | 3             | Product 2     || 3     | 4             | Product 3     |假設(shè)我在Computers類別頁(yè)面中,我想顯示此表中的產(chǎn)品以及所有產(chǎn)品。所以它應(yīng)該首先從Computersand Accessoriesand also獲取產(chǎn)品Keyboards。這是我的類別模型:public function parent() {    return $this->belongsTo(Category::class, 'parent_id');}public function childs() {    return $this->hasMany(Category::class, 'parent_id');}public function products() {    return $this->hasManyThrough(Product::class, Category::class, 'parent_id', 'category_id', 'id');}產(chǎn)品型號(hào):public function categories() {    return $this->belongsTo(Category::class, 'category_id');}查詢:Category::with(['products', 'childs.products'])->where('id', $category->id)->get();返回:{    "id":11,    "parent_id":4,    "name":"Computers",    "products":[        {            "id":2,            "category_id":12,            "title":"Product 1",            "laravel_through_key":11        }    ],    "childs":[        {            "id":12,            "parent_id":11,            "name":"Accessories",            "products":[                {                    "id":1,                    "category_id":13,                    "user_id":1,                    "title":"Product 2",                    "laravel_through_key":12                }            ]        }    ]}上面,它正在轉(zhuǎn)義最后一個(gè)子類別Keyboards。我曾嘗試使用hasManyThrough關(guān)系,但我只得到了產(chǎn)品Computers,Accessories但沒(méi)有達(dá)到Keyboards。因此,如果我在一個(gè)類別中,我想從該類別樹(shù)中獲取所有產(chǎn)品。即使一個(gè)子類別有子類別。我怎樣才能做到這一點(diǎn)?
查看完整描述

4 回答

?
郎朗坤

TA貢獻(xiàn)1921條經(jīng)驗(yàn) 獲得超9個(gè)贊

您可以通過(guò)以下方式修復(fù)它:


建立遞歸關(guān)系:(請(qǐng)?jiān)诖颂巺⒖?Alex Harris 的回答)


// recursive, loads all descendants

// App\Category

public function childrenRecursive()

{

   return $this->childs()->with('childrenRecursive');

}


$data = Category::with(['products', 'childrenRecursive', 'childrenRecursive.products'])->where('id', 2)->get()->toArray();

#Edit:提取產(chǎn)品列表


在控制器中定義Flatten laravel 遞歸關(guān)系集合(樹(shù)集合)函數(shù)


public function flatten($array)

{

        $flatArray = [];


        if (!is_array($array)) {

            $array = (array)$array;

        }


        foreach($array as $key => $value) {

            if (is_array($value) || is_object($value)) {

                $flatArray = array_merge($flatArray, $this->flatten($value));

            } else {

                $flatArray[0][$key] = $value;

            }

        }


        return $flatArray;

}

然后為了只有產(chǎn)品項(xiàng)目


$data = Category::with(['products', 'childrenRecursive', 'childrenRecursive.products'])->where('id', 2)->get()->toArray();


$flatten = $this->flatten($data);


foreach ($flatten as $key => $fl) {

    // eliminate categories from $flatten array

    if (!array_key_exists('category_id', $fl)) {

        unset($flatten[$key]);

    }

}


$product = array_values($flatten);


查看完整回答
反對(duì) 回復(fù) 2022-07-22
?
慕神8447489

TA貢獻(xiàn)1780條經(jīng)驗(yàn) 獲得超1個(gè)贊

一種選擇是使用類似laravel-adjacency-list 的東西。這將允許您使用CTE遞歸加載關(guān)系。


以下是設(shè)置您的步驟(在撰寫(xiě)本文時(shí))


跑composer require staudenmeir/laravel-adjacency-list:"^1.0"

將HasRecursiveRelationships特征添加到您的Category模型中:


use Staudenmeir\LaravelAdjacencyList\Eloquent\HasRecursiveRelationships;


class Category extends Model

{

    use HasRecursiveRelationships;


    ...

}

將您的查詢更改為:


Category::with('descendants.products')->where('id', $id)->first(); //$id being the id of the parent category you want to get.

如果您只想獲取某個(gè)類別中/下的產(chǎn)品,您可以執(zhí)行以下操作:


Product::whereHas('category', function ($query) use ($category) {

    $query->whereIn('categories.id', $category->descendantsAndSelf()->select('id')->getQuery());

})->get();

有關(guān)如何使用的更多信息,laravel-adjacency-list請(qǐng)參閱文檔。


查看完整回答
反對(duì) 回復(fù) 2022-07-22
?
尚方寶劍之說(shuō)

TA貢獻(xiàn)1788條經(jīng)驗(yàn) 獲得超4個(gè)贊

$categories  = Category::whereParentId(null)->with('children.children.children')->get();

并查看您可以使用 foreach 循環(huán)顯示項(xiàng)目


查看完整回答
反對(duì) 回復(fù) 2022-07-22
?
www說(shuō)

TA貢獻(xiàn)1775條經(jīng)驗(yàn) 獲得超8個(gè)贊

如果你使用 ORM 雄辯的關(guān)系


public function categoriesProduct(ProductCategory $category)

{

     $categories = ProductCategory::where('parent_id', $category->id)

                                  ->orWhere('id', $category->id)

                                  ->latest()

                                  ->get();

     return view('categoryProduct', compact('categories', 'category'));

}


查看完整回答
反對(duì) 回復(fù) 2022-07-22
  • 4 回答
  • 0 關(guān)注
  • 304 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)