【问题标题】:Laravel : Three table with relationship to get dataLaravel:三个有关系的表来获取数据
【发布时间】:2018-05-01 06:46:02
【问题描述】:

我的应用中有一个 3 表。products,category,attributes product hasMany category 和 category hasMany 属性关系。

我想以 json 格式获取所有 productsCategory 详细信息和 catgoey attributes 详细信息。我该怎么做?

目前我正在我的控制器中尝试使用此功能:

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');

attributes 表上:

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

我该怎么做?

【问题讨论】:

    标签: laravel laravel-5.5


    【解决方案1】:

    您的产品型号

    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());
        }
    }
    

    【讨论】:

    • 您可以将查询的with 部分简化为with('categories.attributes'),只要您不需要在查询上附加条件语句即可。 if(!empty(...)) 也不是必需的,因为get() 将返回一个空的或非空的Collection。所以toArray() 永远不会失败。
    • SQLSTATE[42S22]: Column not found: 1054 Unknown column 'post_attributes.category_id' in 'where clause' (SQL: select * from post_attributes` where post_attributes.category_id in (1, 2, 3, 4, 5))` 返回
    • @Javed 你定义category_id 为属性表中的外键了吗?
    • @FarazIrfan 是的。您可以检查它添加的问题详细信息
    • @FarazIrfan thanx 工作完美。我的关系模型错误。
    【解决方案2】:

    您可以在模型内部为不同的类别和属性定义关系,需要在 products 模型中为类别(命名类别)定义一个 hasMany 关系,然后在类别(命名属性)模型中定义另一个 hasMany 关系用于属性。之后你可以按照这个过程来准备你的数据数组

    $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_encode 来生成你的 json 数据

    【讨论】:

      【解决方案3】:

      在你的控制器中

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-09-08
        • 2021-08-06
        • 1970-01-01
        • 2020-01-06
        • 2018-10-03
        • 1970-01-01
        相关资源
        最近更新 更多