代码之家  ›  专栏  ›  技术社区  ›  Javed

Laravel:三个表,具有获取数据的关系

  •  1
  • Javed  · 技术社区  · 7 年前

    我的应用程序中有一个3表。 products ,则, category ,则, attributes 产品有多个类别,类别有多个属性关系。

    我想得到所有 产品 具有 Category 详图和catgoey 属性 json格式的详细信息。我该怎么做?

    目前,我正在控制器中尝试此功能:

    public function index()
    {
        $productDetails = Product::all();
        if(!empty($productDetails)) {
            foreach ($productDetails as $key => $value) {
                //print_r($value->id."</br>");
            }
        }
    }
    

    我想要的输出:

           {
           "productInformation": {
                "id": 1,
                "name": "productone",
                "status": "Active",
                "CategoryDetails": [
                    {
                        "id": 1,
                        "product_id": 1,
                        "categoryTitle": "categoryone",
                        "attribute" : [
                            {
                                "id": 1,
                                "product_id": 1,
                                "category_id": 1,
                                "title": "attrib-title-one",
                            },          
                            {
                                "id": 2,
                                "product_id": 1,
                                "category_id": 1,
                                "title": "attrib-title-two",
                            },
                        ]
                    }
                ]
            }
        }
    

    关系:

    在…上 categories 桌子

    $table->foreign('product_id')->references('id')->on('products');
    

    在…上 属性 表格:

     $table->foreign('product_id')->references('id')->on('products');
     $table->foreign('category_id')->references('id')->on('categories');
    

    我该怎么做?

    3 回复  |  直到 7 年前
        1
  •  3
  •   Faraz Irfan    7 年前

    你的 产品型号

    public function categories(){
    return $this->hasMany('App\Category','product_id');
    }
    

    类别模型

    public function attributes(){
    return $this->hasMany('App\Attribute','cat_id');
    }
    

    在您的 控制器

    public function index()
    {
        $productDetails = Product::with('catgories.attributes')->get();
        if(!empty($productDetails)) {
            $jsonData = json_encode($productDetails->toArray());
        }
    }
    
        2
  •  0
  •   Jobayer    7 年前

    您可以在不同类别的模型中定义关系;属性,需要为类别(命名类别)在产品模型中定义一个hasMany关系,然后为属性定义另一个hasMany-in-Categories(命名属性)模型。之后,可以按照此过程准备数据数组

    $productDetails = Product::all();
        if(!empty($productDetails)) {
            foreach ($productDetails as $key => $value) {  
                $productDetails[$key]['categorieDetails'] = $productDetails[$key]->categories;
             foreach ($productDetails[$key]['categorieDetails'] as $key2 => $value2) {  
                $productDetails[$key]['categorieDetails'][$key2]['
                 attribute'] = $productDetails[$key]->categorieDetails[$key2]->attributes;
    
             }
            }
    
        }
    

    然后可以使用json\u encode生成json数据

        3
  •  0
  •   Nadeem Ahmed    5 年前

    在控制器中

    public function index()
    {
        $productDetails = Product::with('catgories.attributes')->get();
        return response()->json($productDetails);
    }