简体   繁体   English

Laravel 检索董事会的所有父母

[英]Laravel Retrieve All Parents For A Board

I have a board that's called "Announcements" which is in a board called "Important Information" in which that is in "Headquarters".我有一个名为“公告”的董事会,它位于一个名为“重要信息”的董事会中,该董事会位于“总部”。 Here is a visual representation of it.这是它的视觉表示。


Headquarters (The main board with no parents) [id = 1, parent_id = null]总部(没有父母的主板) [id = 1, parent_id = null]

↳ Important Information (1st child board of "Headquarters") [id = 2, parent_id = 1] ↳ 重要信息(“总部”的第一个子板) [id = 2, parent_id = 1]

↳ Announcements (1st child board of "Important Information") [id = 3, parent_id = 2] ↳ 公告(“重要信息”的第一个子板) [id = 3, parent_id = 2]


My objective is to get all of the "Announcements" parent boards in a Laravel model.我的目标是在 Laravel 模型中获取所有“公告”父板。


I already constructed a function that gets one parent board.我已经构建了一个获取一个父板的函数。

public function parent()
{
    return $this->where('id', $this->parent_id)->first();
}

If you were to use this function for the "Announcements" board, it would return the results of the "Important Information" board because that is the parent of "Announcements".如果您要将此函数用于“公告”板,它将返回“重要信息”板的结果,因为它是“公告”的父级。

So let's say I were to create a function called listAllParents()假设我要创建一个名为 listAllParents() 的函数

public function listAllParents()
{
    // code here
}

This function would return the results of all parents associated with the specified board.此函数将返回与指定板关联的所有父项的结果。 So if I were to use this function on "Announcements" I would want all parents associated with it to be returned.因此,如果我要在“公告”上使用此功能,我希望返回与它相关的所有父母。

Here is a small code example:这是一个小代码示例:

$board = Board::where('name', 'Announcements')->first();
$getAllParents = $board->listAllParents()->get();

return $getAllParents;

And what should be returned is a list of "Announcements" parent boards, which would be: "Important Information" & "Headquarters".应该返回的是“公告”母板列表,即:“重要信息”和“总部”。 What code should go into the listAllParents() function to make this possible?应该在 listAllParents() 函数中使用哪些代码来实现这一点?

If you need a better explanation, please let me know.如果您需要更好的解释,请告诉我。 I will be more than happy to help.我将非常乐意提供帮助。

Thank you so much for your help.非常感谢你的帮助。

Given your current structure, your best option is to iterate through each Board in your hierarchy until you reach a Board where parent_id === null .鉴于您当前的结构,您最好的选择是遍历层次结构中的每个Board ,直到到达一个Board ,其中parent_id === null For example:例如:

public function listAllParents()
{
    $board = $this;
    $parents = [];

    while (! is_null($board->parent_id)) {
        $board = $board->parent();
        $parents[] = $board;
    }

    return $parents;
}

This however is highly inefficient since you're executing a query to find the node at each level of the hierarchy.然而,这是非常低效的,因为您正在执行查询以在层次结构的每个级别查找节点。

An alternative approach would be to use a Nested Tree data structure, and there are various packages available to implement this functionality in Laravel.另一种方法是使用嵌套树数据结构,在 Laravel 中有多种包可以实现此功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM