php - Laravel 8 嵌套模型关系

标签 php laravel-8 eloquent-relationship

enter image description here这是我的第一个 Laravel 项目,请耐心等待。我正在尝试显示父->子层次结构(非递归),类似于下面的内容:

  • 类别 1
  • ----列表项
  • ----------资源 1
  • ----列表项
  • ----------资源 2
  • 类别 2
  • -----列出第1项
  • -----列出第 2 项....等

我不知道如何编写查询来获取链接到 subStatement 的资源,而不是顶级类别,基本上是与 SubStatement 相关的内部联接。目前显示如下;

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

//主题模型

{
   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
}

Statement.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
}

Resource.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();  

关于php - Laravel 8 嵌套模型关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70704555/

相关文章:

php - 如何使用ajax在模态弹出窗口中显示php

php - CakePHP 分页排序包含数据

Laravel 8 Sanctum SPA Auth - 未按要求设置 session 存储

php - Laravel 中的 SerializableClosure 错误 - 您的序列化闭包可能已被修改,反序列化是不安全的

laravel - 如何通过 Laravel 的 Eloquent 关系和预加载连接 3 个表?

php - Laravel orderBy 关系

php - 为什么我的 .htaccess 在部署时停止工作

当随机化超过 1 个随机数组键时,PHP array_rand 无法正常工作

shopify - 如何获取/生成店面API的访问 token