簡體   English   中英

Laravel 8 個嵌套的 Model 關系

[英]Laravel 8 nested Model Relationships

在此處輸入圖像描述 這是我的第一個 Laravel 項目,請多多包涵。 我正在嘗試顯示父->子層次結構(非遞歸),類似於以下內容:

  • 第一類
  • - - 項目清單
  • ----------資源1
  • - - 項目清單
  • ----------資源2
  • 第 2 類
  • -----清單項目1
  • -----列出項目2....等

我無法理解如何編寫查詢以獲取鏈接到子語句的資源,而不是頂級類別,基本上是與子語句相關的內部連接。 目前是這樣顯示的;

    {
        "id": 1,
        "name": "Category 1: Practising plumbing overview",
        "description": "<p>Category 1: Practising plumbing overview</p>",
        "created_at": "2022-01-06 15:48:49",
        "updated_at": "2022-01-06 15:48:49",
        "deleted_at": null,
        "plumbing_area_id": 1,
        "category_id": 1,
        "SubStatement": [
            {
                "id": 1,
                "title": "Plying narrative for Category 1",
                "description": "<p>Body of content.</p>",
                "theme_id": 1,
                "plumbing_area_id": 1
            },
            {
                "id": 2,
                "title": "Lay narrative for Category 1",
                "description": "<p>body description.</p>",
                "theme_id": 1,
                "plumbing_area_id": 1
            }
        ],
        "resources": []
    },
    {
        "id": 2,
        "name": "Category 2: AAV Practising plumbing introduction",
        "description": "<p><strong>Category 2:</strong> AAV Practising plumbing introduction</p>",
        "created_at": "2022-01-06 15:49:12",
        "updated_at": "2022-01-06 15:49:12",
        "deleted_at": null,
        "plumbing_area_id": 1,
        "category_id": 1,
        "SubStatement": [
            {
                "id": 3,
                "title": "Plying narrative for Category 2",
                "description": "<p>Body of content;</p>",
                "theme_id": 2,
                "plumbing_area_id": 1
            },
            {
                "id": 4,
                "title": "Lay narrative for Category 2",
                "description": "<p>Body of content</p>",
                "theme_id": 2,
                "plumbing_area_id": 1
            }
        ],
        "resources": []
    },
    {
        "id": 3,
        "name": "Category 3: AAV Practising plumbing design",
        "description": "<p><strong>Category 3: </strong>AAV Practising plumbing design</p>",
        "created_at": "2022-01-06 15:49:47",
        "updated_at": "2022-01-06 15:49:47",
        "deleted_at": null,
        "plumbing_area_id": 1,
        "category_id": 1,
        "SubStatement": [
            {
                "id": 5,
                "title": "Plying narrative for Category 3",
                "description": "<p>Body of content;</p>",
                "theme_id": 3,
                "plumbing_area_id": 1
            },
            {
                "id": 6,
                "title": "Lay narrative for Category 3",
                "description": “Blah blah</p>",
                "theme_id": 3,
                "plumbing_area_id": 1
            }
        ],
        "resources": []
    },
    {
        "id": 4,
        "name": "Category 4: another “category,
        "description": “Body</p>",
        "created_at": "2022-01-06 15:50:04",
        "updated_at": "2022-01-06 15:50:04",
        "deleted_at": null,
        "plumbing_area_id": 1,
        "category_id": 1,
        "SubStatement": [
            {
                "id": 7,
                "title": "Plying narrative for Category 4",
                "description": "<p>sdfsdfsdf;</p>",
                "theme_id": 4,
                "plumbing_area_id": 1
            },
            {
                "id": 8,
                "title": "Lay narrative for Category 4",
                "description": "<p> sdfsdfsdfsdf</p>",
                "theme_id": 4,
                "plumbing_area_id": 1
            }
        ],
        "resources": []
    },
   ]

我正在尋找類似下面的東西,請注意資源節點的位置:

[
  {
    "id": 1,
    "name": "Category 1: Practising plumbing overview",
    "description": "<p>Category 1: Practising plumbing overview</p>",
    "created_at": "2022-01-06 15:48:49",
    "updated_at": "2022-01-06 15:48:49",
    "deleted_at": null,
    "plumbing_area_id": 1,
    "category_id": 1,
    "SubStatement": [
      {
        "id": 1,
        "title": "Plying narrative for Category 1",
        "description": "<p>Body of content.</p>",
        "theme_id": 1,
        "plumbing_area_id": 1,
        "resources": [
          {
            "id": 1,
            "resource_title": "The name of the resource"
          }
        ]
      }
    ]
  }
]

//主題 Model

{
   return $this->hasMany(Statement::class, 'theme_id', 'id');
}
public function resources()
{
 return $this->belongsToMany(Resource::class);
}

//下面的Controller正在輸出上面的JSON

{
 $output = Theme::where ( 'category_id', '=', $category_id )
                    ->with(['subStatement' =>
                        fn ($query) =>
                        $query->select('id','title','description','theme_id','therapy_area_id')

                    ])
                    ->with(['resources' =>
                        fn ($query) =>
                        $query->select('id','temporary_url')])
                    ->get();
}

// 我試圖寫這樣的東西。 它引發了“完整性約束違規:字段列表中的 1052 列 'id' 不明確”錯誤

{
    return $this->hasOneThrough(Statement::class, Resource::class);
}

您的主要問題是您需要嵌套查詢。

  • 我將首先根據您的模式將關系添加到您的模型中。

  • 然后,如果我了解您要正確實現的目標,我將為嵌套添加基本查詢。

楷模

主題.php


public function subStatements()
{
    return $this->hasMany(Statement::class); // 1-N
}

public function statements()
{
    return $this->belongsToMany(Statement::class); // N-N
}

public function resources
{
    return $this->belongsToMany(Resource::class); // N-N
}

聲明.php


public function theme()
{
    return $this->belongsTo(Theme::class); // 1-N
}

public function themes()
{
    return $this->belongsToMany(Theme::class); // N-N
}

public function resources
{
    return $this->belongsToMany(Resource::class); // N-N
}

資源.php


public function themes()
{
    return $this->belongsToMany(Theme::class); // N-N
}

public function statements()
{
    return $this->belongsToMany(Statement::class); // N-N
}

詢問


$themes = Theme::query()
               ->where('category_id', $category_id)
               ->with([
                  'subStatements' => fn ($query) => $query->with('resources') 
               ])
               ->get();  

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM