功能是找到一个条目,但不返回它

时间:2015-12-23 16:01:23

标签: php laravel-5

我想遍历数据库字段,直到找到所有孩子的母亲。当我回复$page->id函数中的findMother()(而不是返回它)时,它会给我正确的page id,但它不会将其返回到第二个函数。

private function findMother($id) {
    $page = Page::find($id);
    if($page->parent_id != 0 || $page->parent_id != null) {
        $this->findMother($page->parent_id);
    } else {
        // if I echo the $page->id here it shows me the correct mother page id
        return $page->id;
    }
}

private function loadSubPages($api) {
    $page = Page::where('api', $api)->first();
    $mother = $this->findMother($page->id);
    die('mother: ' . $mother); // $mother is empty
}

有人知道我在这里缺少什么吗?

2 个答案:

答案 0 :(得分:1)

您应该返回函数调用的结果:

DO

return $this->findMother($page->parent_id);

而不是

$this->findMother($page->parent_id);

这样您将返回结果

答案 1 :(得分:0)

感谢jiboulex我解决了以下变化:

private function findMother($id) {
    $page = Page::find($id);
    $return = $page->id;
    if($page->parent_id != 0 || $page->parent_id != null) {
        return $this->findMother($page->parent_id);
    } else {
        return $return;
    }
}
相关问题